Problem with cout with multiple namespaces.

I am trying to practice using namespaces that each have a class of the same name. I tried to create two constant integers of the same name and print the one in Namespace2 by using the using namespace Namespace2 declaration, however,I get the following error: 'intDemonstration' was not declared in this scope. The code Car car; car.startcar(); works, but for whatever reason the cout statement does not. Also, if I make my functions void startCar(); private and I try to use them in the main file via the void Car::car.startCar(); syntax it gives me an error saying: 'car' is not a member of 'Namespace2::Car'. Why does the cout not work and why can't I make my functions in the two namespaces private? The car.startCar function call works when it is not private. but doesn't when I switch the functions to private. This means that when it is not private it recognizes that the default is Namespace2, but when it is private it doesn't understand Car::car.startCar.

Main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
#include <iostream>
#include "Car.h"
#include "Vehicle.h"

using namespace std;
using namespace Namespace2;

int main() {
	Car car;
	car.startCar();

	cout << intDemonstration << endl;

	return 0;
}

Car.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "Car.h"

namespace Namespace1{

void Car::stateInt(){
	cout << intDemonstration << endl;
};

void Car::startCar(){ cout << "car Started." << endl;};

Car::Car() {

}

Car::~Car() {

}

};

Car.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

#ifndef CAR_H_
#define CAR_H_

namespace Namespace1{

class Car {
public:
	const int intDemonstration = 88;
	void stateInt();
	void startCar();
	Car();
	virtual ~Car();
};

}

#endif /* CAR_H_ */

Vehicle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Vehicle.cpp
 *
 *  Created on: Jun 17, 2021
 *     
 */

#include "Vehicle.h"

namespace Namespace2 {

void Car::stateInt(){
	cout << intDemonstration << endl;
}

void Car::startCar(){
	cout << "Car started." << endl;
}

Car::Car() {

}

Car::~Car() {

}

} /* namespace Namespace2 */

Vehicle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Vehicle.h
 *
 *  Created on: Jun 17, 2021
 *     
 */

#include <iostream>
using namespace std;

#ifndef VEHICLE_H_
#define VEHICLE_H_

namespace Namespace2 {

class Car {
public:
	const int intDemonstration = 77;
	void stateInt();
	void startCar();
	Car();
	virtual ~Car();
};

} /* namespace Namespace2 */

#endif /* VEHICLE_H_ */


perhaps you meant object.intdemo....

car is not a member of NS2. Its right there in main, outside the namespace.
namespace does not give you access to private. Namespace just avoids name collisions so you can have 2 of the same name work together via :: resolution to choose which one (or using, which pulls it into scope).
Last edited on
I tried adding Car car to the namespaces as you said. The first time I tried it it seemed to work, but then I decieded to add a car2 to test the stateInt functions. When I tried to use car2 it did not like it and then I commented out what I had changed, but then car was no longer working.

Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include "Car.h"
#include "Vehicle.h"

using namespace std;
using namespace Namespace2;

int main() {

	car.startCar();

	car2.stateInt();

	//cout << intDemonstration << endl;

	return 0;
}


Car.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

#ifndef CAR_H_
#define CAR_H_

namespace Namespace1{

class Car {
public:
	void startCar();
	const int intDemonstration = 88;
	void stateInt();
	Car();
	virtual ~Car();
};

Car car;
Car car2;
}

#endif /* CAR_H_ */ 


Vehicle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Vehicle.h
 *
 *  Created on: Jun 17, 2021
 *      Author: pclay
 */

#include <iostream>
using namespace std;

#ifndef VEHICLE_H_
#define VEHICLE_H_

namespace Namespace2 {

class Car {
public:
	void startCar();
	const int intDemonstration = 77;
	void stateInt();
	Car();
	virtual ~Car();
};

Car car;
Car car2;
} /* namespace Namespace2 */

#endif /* VEHICLE_H_ */
I was not trying to suggest that you make global variables in the namespace. Its ok to test with, of course, but something I would usually avoid as much as global namespace global variables. It is only a slight improvement to put them in a NS.

Hang on, I don't see the problem...
I get a cut down version to work fine... what is your error message?

this works for me, and its close to what you did, but I didn't want to make 6 files for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

namespace Namespace2 {

class Car 
{
public:
	void startCar(){cout << "started\n";}
	const int intDemonstration = 77;
	void stateInt(){cout << "i am ent\n" << intDemonstration << endl;}	
};

Car car;
Car car2;
} /* namespace Namespace2 */

using namespace Namespace2;

int main()
{
  car2.stateInt();
} 
Last edited on
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: src\Namespaces try two.o:Namespaces try two.cpp:(.bss+0x0): multiple definition of `Namespace1::car'; src\Car.o:Car.cpp:(.bss+0x0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: src\Namespaces try two.o:Namespaces try two.cpp:(.bss+0x8): multiple definition of `Namespace1::car2'; src\Car.o:Car.cpp:(.bss+0x8): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: src\Vehicle.o:Vehicle.cpp:(.bss+0x0): multiple definition of `Namespace2::car'; src\Namespaces try two.o:Namespaces try two.cpp:(.bss+0x10): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: src\Vehicle.o:Vehicle.cpp:(.bss+0x8): multiple definition of `Namespace2::car2'; src\Namespaces try two.o:Namespaces try two.cpp:(.bss+0x18): first defined here
Hi,

You are being a little too cute with the namespaces, you have the same variable in both.

Namespaces are supposed to be for separating decent sized sections of code to avoid name clashes. Yours is too fine grained, IMO.

One could have a namespace for all the vehicle types - whether they be cars, motorbikes, tractors, formula 1, aircraft etc. If there were lots of cars and aircraft, one might consider having separate namespaces for them, but only if was worth it.
delete all the intermediate files do a full from scratch rebuild.
if it still does it, I do not see why, you have include guards and it all looks correct...
Thank you both.
Topic archived. No new replies allowed.