Hi,
I can not run this program since yesterday.
What I do wrong?
//a.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
#ifndef _A_H_
#define _A_H_
class B;
usingnamespace std;
class A {
public:
B *b;
int i;
A(int ii);
A(const A &aa);
~A();
void display1()const;
};
#endif
#pragma once
#ifndef _B_H_
#define _B_H_
#include <iostream>
usingnamespace std;
class A;
class B{
public:
A *a;
int i;
B(int ii);
B(const B &bb);
~B();
void display2()const;
};
That was what I going to suggest (as in ne555 post): make one of the classes responsible for creating the other but not both like you were doing in the original post.
Because all the files that include that header will be using namespace std.
This could cause conflicts with functions in others namespaces.
(Basically you are killing the reason of use a namespace at all.)
Suppose that you create a class vector (like in maths or physics). That class will support operations like + - * between vector or vector and scalar, and a lot of other methods. Note that vector is a proper name for that class.
To avoid collisions you put that class in the namespace Math.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Math.h"
#include "header_with_using_namespace_std.h"
#include <vector>
//others headers
using Math::vector; //Is going to be used a lot
typedef std::vector<int> array_int; //to avoid confusion
int main(){
//sorry about the names, but that is not important (you could have an array and a math vector in the same source)
vector<4> rot;
array_int arr;
return 0;
}
The compiler yields
reference to 'vector' is ambiguous
candidates are: template<unsigned int n> class Math::vector
/usr/include/c++/4.4/bits/stl_vector.h:170: error: template<class _Tp, class _Alloc> class std::vector
'rot' was not declared in this scope
So now I will have to change all the declarations of vector<n> to Math::vector<n>