How to use namespace???

I have two header files, A, B.

A.h
1
2
3
4
5
6
7
namespace test
{
  struct info
  {
    int a;
   }
}


B.h
1
2
3
4
5
#include "A.h"
int main
{
  test::info.a = 10;
}


However, it has an error. I don't quite understand how to use namespace.

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace test
{
    struct info
    {
        int a;
    };
}

int main()
{
    test::info objTest;

    objTest.a = 10;
}


I don't think a main function should be in a header.
Last edited on
I don't recommend asking for help with code and just saying that there is an error somewhere. Luckily this is short code and you used code tags. Like Olysold said you shouldn't be putting your global main in a header file, it has to be in a .cpp file. I don't see anything wrong with your code regarding namespace. You didn't make an object for your struct. You can't just use the class. Imagine you had a class for cars in general. The class would be the blueprint on details that you know all cars have. How many wheels, length, weight etc. The objects you declare however are the actual cars. You have to declare the object then use it.
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
31
#include<iostream>
#include<string>
class Car
{
public:
	Car(int l, int w,std::string n)
	{
		length = l;
		weight = w;
		name = n;
	}
	void print_details()
	{
		std::cout << "Car: " << name << std::endl;
		std::cout << "Weight: " << weight << std::endl; // You pick your units of measurement lol
		std::cout << "Length: " << length << std::endl;
	}
private:
	int length;
	int weight;
	std::string name;
}

int main()
{
	Car Car1(10, 2000,"Toyota");
	Car1.print_details();
	Car Car2(16, 3400,"Limo");
	Car1.print_details();
	return 0;
}

Topic archived. No new replies allowed.