A
Aneel
How to make a class with name 'count' in c++?
Aneel said:How to make a class with name 'count' in c++?
class count {};
Aneel said:But when I make an object of the class with name count, the compiler
says ('count' undeclared)..
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.
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);
Aneel said:But when I make an object of the class with name count, the compiler
says ('count' undeclared)..
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).
Juha Nieminen said:Which is exactly why the expression "using namespace std;" should beGil 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);
removed from the C++ standard and cause a compiler error.
namespace std;«. The code should:
Paavo said:(e-mail address removed)-berlin.de (Stefan Ram) wrote in (e-mail address removed)-berlin.de:
For teaching purposes, I would like to see an example ofJuha 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.
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);
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
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.