Visual Studio 2012 Issue

I am working on a small project and I keep coming up on this same issue with Visual Studio. I am not sure if this is a Visual Studio specific issue or maybe just something that I am not seeing but either way I am really starting to hate VS.

The issue that I am having is in my header files when I use the pre process directive #include, it still will not recognize the objects found in those libs. For instance, in one of my header files I used #include <string> and I tried to create a method that has a parameter with a string and it says that string is undefined. Yet, in another header file I include it and it works perfectly. I had this issue with another header file with a class file that I defined. Has anyone else had this issue? If anyone could offer any assistance that would be great!

Erock
closed account (Dy7SLyTq)
are you bringing in string from the std namespace?
Its possible that string has been included at sometime before in the preprocessing stage, but somehow its scoping hasn't been given to the header file, normally through a cyclic dependency. What is happening is that the header file has been included once before, and then when you try to include it again the include guard activates, preventing multiple definitions. Then again, I don't exactly know how you did it (and what can be done to fix it), so showing your source code could help. It could also help in that I might be completely wrong, and the source code could clear up my theory as being incorrect.
It sounds like you are probably right NTS. As I have included it more than once. I was literally now working on this project and I again tried to include another file and boom, happened again, except now it is breaking my whole project. Do you think if maybe I switched it from the #pragma once to the classic way of doing it it would help prevent this issue? I swear I hate VS.


Sorry that I am not including any code. I really do not want to as it is really bad code and I am extremely embarrassed of it. I have not yet gotten the hang of organizing larger projects. Any advice with this ? Ha. Anyways thanks for the response and hope to hear from you soon.

Erock
Okay so I switched them all to the #ifndef _blah
#define _blah preprocessor directives and I got the same issue. I do not understand why I am not able to do this.
Did you write "string" or "std::string"?
usually just string, but even if, i have had this same issue with class files that I created. So I am under the impression the namespace is not the issue. I appreciate the idea though and the input.
You're under an invalid impression. If a car explodes at night and a nuclear power plant explodes at night, by your logic things only explode at night.

The namespace is very likely an issue - have you tried writing std::string instead?

As for having undefined symbols with your own custom classes, a likely culprit is not namespace but instead cyclic dependencies.

Don't assume that the same kind of error means the same kind of problem causing it.
Basically to rephrase what has happened. First off, I am knee deep in this project and I admit, the code for it has become a hot mess. I have had to create so many work arounds that almost all the variables are public and static ha. But please excuse that. I am a computer science major and at school they fail to teach us how to manage larger projects. But enough of that, the issue is for some reason whenever I include a certain header file, even the ones that I created in my project, I will attempt to create an object of this class or I will attempt to use the object type in a method parameter and it will say the type is undefined. Visual Studio seems to be picking and choosing the files it does not like. I have the same include files in other headers and objects belonging to this class and it works great. I have no idea why it would let me do it in some, but then the others it will not. Does not make sense to me.

Btw, I am working on a clone of Duck Hunt. A very crappy clone. I am just doing it for practice but turns out this was waaaay over ambitious for me. I am on day 9 of working on this. Taking me way too long.

Thanks again.

Erock
Are there any cyclic dependencies? That is, headers that include each other? The cycle may even be larger than just two headers - you could have a 7 header cycle without realizing it.
Last edited on
Please don't take shots at my logic. I am really overall new to programming so I do make certain assumptions that may be wrong but only because I can only go off what I know and what I have come across before. What are these cycle dependencies you speak of?
Yes, I do totally have headers that include each other. This project has gotten disgusting. Is this not legal? Something I should do when I know this is going to happen?
Here. I've slapped together a little crash course for you, just so everyone is on the same page.

.h and .cpp file extensions
Header files (.h) typically contain class declarations and prototypes for functions, variables and other identifiers.
This means they typically don't contain any actual execution code - but they can, and sometimes, under very special circumstances, have to.

Source code files (.cpp) contain the implementations (function body) of previously declared things (like functions, variables, whatever).
Therefore, it's save to say that USUALLY a .cpp file has an accompanying .h file (usually by the same name), and vice versa.

If another file needs to know about something you've declared elsewhere, you include the header file that contains the needed definitions.

"include guards/macro guards"
If you include a header file multiple times in a project in different files, you'll get compilation errors regarding name clashes.
Consider the following scenario:

class Baby needs to know about class Person.
class Adult also needs to know about class Person.

Baby and Adult would include the necessary Person declarations.
The second time the Person declarations show up in the compilation process, the compiler complains because the Person declarations already exist.

This is why it becomes necessary to use so called "include/macro guards", which ensure that the declarations they encapsulate only ever show up once in the compilation process.
One way of doing this is by using the preprocessor #ifndef (if no definition) control structure like so.

1
2
3
4
5
6
7
8
#ifndef _PERSON_H_//(if there's no definition present for _PERSON_H_)
#define _PERSON_H_//(then define it)

class Person {
/**/
};

#endif//(end of the #if control structure) 


#include<> vs #include""
Typically, you use #include <> for headers that are located in the compiler/IDE include directory (the standard files that come with your compiler/IDE).
You use #include "" for local project files (files which you've created, and added to the project).

cyclic dependancies
Sometimes, class Foo needs to know about class Bar, and class Bar needs to know about class Foo.

In this case, one can use a forward declaration/class prototype, like so:

foo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _FOO_H_
#define _FOO_H_

#include "bar.h"

class Bar;//prototype for Bar

class Foo {
public :
	Foo() {/**/}
	int foo;
	Bar* bar;

};

#endif 


bar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _BAR_H_
#define _BAR_H_

#include "foo.h"

class Foo;//prototype for Foo

class Bar {
public :
	Bar() {/**/}
	int bar;
	Foo* foo;

};

#endif 
Last edited on
Thank you very much for putting so much effort in to explaining! Very helpful. I will try this!
Erock
Topic archived. No new replies allowed.