Is not a namespace name

Is not a namespace name

I am tying to use some .Net namespaces in my c++ code. From here I have found, how can I do that. In my code I have:

If I run it I get these errors:

what should I do to solve this problem? By the way, I have read this and this posts, but I could not understand, what should I do with /clr in my cpp code?

Thread Tools
Search Thread
Display
  • Linear Mode
  • Switch to Hybrid Mode
  • Switch to Threaded Mode

"is not a class or namespace name" error

I’m trying to figure out why this piece of code, while simple, just refuses to compile in Visual C++ 2010. I have these three example files (two headers, and one main CPP code file). I’d like to use an enum defined in one of the header files (main.h) as a return type for a function in the other (foo.h).

When I try to build the project, I get error C2653: ‘main’ : is not a class or namespace name.

I don’t think the problem is due to main.cpp, or any of the files for that matter. I feel this should work, but isn’t because of something the compiler is missing. Can anyone shed some light on this?

It looks like you have a name collision: you have a class in the global namespace named main, but the global main function is also named main (duh). Rename your class, or use your own namespace.

Ah, I can see how that could be a problem. Stupid mistake on my part. However, after renaming main and correcting all relative lines to a new name "newtest", the error still occurs. Any other insight?

Oh, I did not notice this earlier, but you also have a circular include problem: main.h includes foo.h which includes main.h

It looks like main.h does not need to include foo.h, so remove that header inclusion.

Do circular include problems exist even though I have #ifndefs present in both headers?

Also, what if main/newtest has a datamember that needs something from foo? A method that requires the output from foo.FunkyFunction perhaps? Is there some way to satisfy the dependence of foo on main AND main on foo at the same time without running into this circular include problem?

Читайте также:  Телефон висит на загрузке

(PS, thank you so much for your quick and helpful replies)

Kind of. What is happening here is that when main.h is included, it includes foo.h. foo.h includes main.h, but due to the guard, that include effectively does not happen. Now, you use the class name for the enum. but the class has not been defined yet because it will only be defined later in main.h.

If feasible, you would use a forward declaration instead of including the header. Otherwise, you may have to re-design.

I think I get it now. Thanks for taking the time to help me out! It looks like I’ll just have to redesign my program. I’m thinking separate the enum from the other two classes/headers.

I have one quick, semi-related question involving enums that are public datamembers of other classes. To keep things simple, use the previous program as an example and supposing the previous error did not apply, how could I make FunkyFunction return aNewEnum’s blue value? The two ways I originally thought would work were along the lines of:

Neither of these work. As you can tell, I’m not very sure when to use :: and when to use . . (still sort of new to C++) What is the correct way to do this?

Last edited by blakeo_x; 09-16-2011 at 02:40 AM .

While compiling the code in set of files below, I get the following error:

Here is the code:

Guys, I really need to solve this ASAP. I would really appriciate all of your help 🙂

    3 Contributors

  • forum13 Replies 417 Views 10 Hours Discussion Span
  • commentLatest Post 11 Years Ago by nirali35
Читайте также:  Лучшие часы на алиэкспресс отзывы

> friend int Member::checkOut();
> friend int Member::checkIn();
Replace these two lines with this:

Edward doesn’t have a solid reference on hand, but I don’t think you can make a single non-static method be a friend. The friend has to be a non-member function or a whole class.

Thanks Edward, it works! 🙂

> friend int Member::checkOut();
> friend int Member::checkIn();
Replace these two lines with this:

Edward doesn’t have a solid reference on hand, but I don’t think you can make a single non-static method be a friend. The friend has to be a non-member function or a whole class.

Edward doesn’t have a solid reference on hand, but I don’t think you can make a single non-static method be a friend. The friend has to be a non-member function or a whole class.

Actually a member function can also be friend (just like the two functions in the above Book class), quoting MSDN .

A friend function is a function that is not a member of a class but has access to the class’s private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class’s scope, and they are not called using the member-selection operators (. and ->) unless they are members of another class.

I am with you. I had also read that!

But then why my above code is not doing any good?

Actually a member function can also be friend (just like the two functions in the above Book class), quoting MSDN .

A friend function is a function that is not a member of a class but has access to the class’s private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class’s scope, and they are not called using the member-selection operators (. and ->) unless they are members of another class.

Читайте также:  Выравнивание ячеек в таблице word 2016

mitrmkar is right, you just have to be careful about the circular dependency to get a clean compile:

You are needlessly including header files (.h) inside other header files. Try to do that as little as ever possible (in general). Instead of pulling in a whole header file inside another, learn to use forward declarations. For example, in guest.h you can use a forward declaration instead of pulling in book.h, i.e.

and in guest.cpp you then would have the include

The fixed headers are below, you need to add #include "book.h" in the guest.cpp

Thank you all so much!

You are needlessly including header files (.h) inside other header files. Try to do that as little as ever possible (in general). Instead of pulling in a whole header file inside another, learn to use forward declarations. For example, in guest.h you can use a forward declaration instead of pulling in book.h, i.e.

and in guest.cpp you then would have the include

The fixed headers are below, you need to add #include "book.h" in the guest.cpp

But after modifying my checkOut and checkIn methods in Member class, I get the following errors:

Isn’t class Book already known to Guest.h which is indirectly known to Member.cpp??

Here is modified Member.h and Member.cpp files:

But after modifying my checkOut and checkIn methods in Member class, I get the following errors:

Isn’t class Book already known to Guest.h which is indirectly known to Member.cpp??

With the setup you have, Book is declared only, so the compiler is unable to utilize the Book class’s code i.e. you have to add #include "book.h" in the member.cpp.

Well, if I include Book.h in Member.cpp, this is what I get:

Book::checkOut and Book::checkIn are friends of Member!!

Ссылка на основную публикацию
Adblock detector