class with name 'count'

G

Gil Trude

Becareful with the word count, as <algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout << std::count(arr, arr+10, 5);
 
I

Ian Collins

Gil said:
Becareful with the word count, as<algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout<< std::count(arr, arr+10, 5);

Which is exactly why the expression "using namespace std;" should be
removed from the C++ standard and cause a compiler error.

There was me thinking I was the only person who hated it!
 
I

Ian Collins

Becareful with the word count, as<algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout<< std::count(arr, arr+10, 5);

The standard library uses lots of names, hence the namespace...
 
R

Rolf Magnus

Aneel said:
But when I make an object of the class with name count, the compiler
says ('count' undeclared)..

Then you're doing something wrong. What that is, I don't know, since you
didn't show how you're trying.
 
A

Abhishek Padmanabh

Juha Nieminen said:
No, "using namespace std;"

With other namespaces, the need/usefulness is arguable. It depends a
lot on the length of the namespace names being used, as well as how much
the names inside the namespace are used (eg. if you need to use things
inside that namespace 5 times in one single expression, then it might be
arguable that it makes it easier if you don't have to write three nested
namespace prefixes each time).

.... and will add up to the traffic on newsgroups/forums just because the
developer will find it works for other namespaces but not for std, making a
generic feature of a language have exceptional rules. :)
 
S

Stefan Ram

Juha Nieminen said:
Gil said:
Becareful with the word count, as <algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout << std::count(arr, arr+10, 5);
Which is exactly why the expression "using namespace std;" should be
removed from the C++ standard and cause a compiler error.

For teaching purposes, I would like to see an example of
source code that fails spectacularly (under every C++
implementation) due to an inconspicuous usage of »using
namespace std;«. The code should:

- fail to behave as expected by a beginnner reading it,

- this failure should be caused by the usage of
»using namespace std;«,

- but it should not be obvious to a beginner that this
causes the problem,

- so I would get the message »using "using namespace
std;"« might cause bugs that are hard to find for
beginners.
 
S

Sjouke Burry

Paavo said:
(e-mail address removed)-berlin.de (Stefan Ram) wrote in (e-mail address removed)-berlin.de:
Juha Nieminen said:
Gil Trude wrote:
Becareful with the word count, as <algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout << std::count(arr, arr+10, 5);
Which is exactly why the expression "using namespace std;" should be
removed from the C++ standard and cause a compiler error.
For teaching purposes, I would like to see an example of
source code that fails spectacularly (under every C++
implementation) due to an inconspicuous usage of »using
namespace std;«. The code should:

- fail to behave as expected by a beginnner reading it,

- this failure should be caused by the usage of
»using namespace std;«,

- but it should not be obvious to a beginner that this
causes the problem,

- so I would get the message »using "using namespace
std;"« might cause bugs that are hard to find for
beginners.


Does this qualify?


#include <iostream> // for cout
#include <stdlib.h> // for rand()
#include <algorithm> // for generate
#include <math.h> // for sin()/cos()
using namespace std;

const double pi = 3.141592653589793;

/// Rotate a point (x, y) by an angle alpha
void rotate(double* x, double* y, double alpha) {
double x1 = cos(alpha)* (*x) + sin(alpha)* (*y);
double y1 = -sin(alpha)* (*x) + cos(alpha)* (*y);
*x = x1;
*y = y1;
}

int main() {
const int N=10;
double angles[N];
// generate N random angles in degrees
generate(angles, angles+N, rand);
// rotate the same point by each angle
for (int i=0; i<N; ++i) {
double x=10.0, y=20.0;
// convert angle to radians
double alpha=angles*pi/180.0;
// rotate the point
rotate(&x, &y, &alpha);


Check with your function ,alhpa need to be value, NOT adress..........
 
S

Sjouke Burry

Paavo said:
Paavo said:
(e-mail address removed)-berlin.de (Stefan Ram) wrote in (e-mail address removed)-berlin.de:

Gil Trude wrote:
Becareful with the word count, as <algorithm> uses the same word.
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
cout << std::count(arr, arr+10, 5);
Which is exactly why the expression "using namespace std;" should be
removed from the C++ standard and cause a compiler error.
For teaching purposes, I would like to see an example of
source code that fails spectacularly (under every C++
implementation) due to an inconspicuous usage of »using
namespace std;«. The code should:

- fail to behave as expected by a beginnner reading it,

- this failure should be caused by the usage of
»using namespace std;«,

- but it should not be obvious to a beginner that this
causes the problem,

- so I would get the message »using "using namespace
std;"« might cause bugs that are hard to find for
beginners.

Does this qualify?


#include <iostream> // for cout
#include <stdlib.h> // for rand()
#include <algorithm> // for generate
#include <math.h> // for sin()/cos()
using namespace std;

const double pi = 3.141592653589793;

/// Rotate a point (x, y) by an angle alpha
void rotate(double* x, double* y, double alpha) {
double x1 = cos(alpha)* (*x) + sin(alpha)* (*y);
double y1 = -sin(alpha)* (*x) + cos(alpha)* (*y);
*x = x1;
*y = y1;
}

int main() {
const int N=10;
double angles[N];
// generate N random angles in degrees
generate(angles, angles+N, rand);
// rotate the same point by each angle
for (int i=0; i<N; ++i) {
double x=10.0, y=20.0;
// convert angle to radians
double alpha=angles*pi/180.0;
// rotate the point
rotate(&x, &y, &alpha);

Check with your function ,alhpa need to be value, NOT adress..........


But the program compiles fine with no warnings. I pretended this to be a
mistake a beginner would do.

cheers
Paavo

The compiler cannot read your mind.
It does what you tell it to do, not what you want it to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top