Public Group

# std::vector problem

This topic is 4412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I have tried to search for a solution to this problem but no luck so far. I'm trying to make a std::vector of a class I have made myself in another class. Ok, not easy to explain :) Here's some code, (simplified for the presentation of my problem): Nationclass.h
class cWarhead
{
public:

int DamagePotential;

};

class nationclass
{
public:

nationclass( );
~nationclass();

std::string sCountryName, sCityName[3];

int targetcountry, targetcity;

};


This compiles fine but when I make an instance of nationclass I can not access the Warheads vector. I can access the strings and the ints declared in the .h file though. Am I doing soething terribly wrong here, or do you have to see more code? A

##### Share on other sites
Is this a compile or runtime error? What you have in your example compiles ok for me so...

Are you sure you don't want 'std::vector<cWarhead>' ? In the way that you have it written, 'Warheads' is a vector of pointers, so it will not allocate or manage any cWarheads itself -- only the pointers to them. So maybe you're left with dangling pointers which is giving you access violations? I don't know.

##### Share on other sites
More code is always good. When you say you can't access the Warheads, what do you mean exactly? I can't see how you would be getting an "access denied since member is private" error.

Could you post an example of you trying to access the Warheads and the error that is generated when you do?

##### Share on other sites

It is a compile error. It occurs when i try to access the Warheads vector. It is very strange. Here is something I tried in my constructor:

nationclass::nationclass(  ){		cWarhead *testwarhead = new cWarhead();	Warheads.push_back(testwarhead);}

Now this compiles fine. But when I'm working with this my IDE (VS 2005), does not recognize Warheads. When I right click on Warheads and choose "Go to definition" it gives me an error saying "The symbol 'Warheads' is not defined". Isn't that strange considering it actually compiles?

Ok, and then in my main function I'm making an instance of nationclass:

USA = new nationclass( );USA->Warheads... //whatever I try here gives me a compile error saying Warheads is not defined

If this is not enough I will post more code.

##### Share on other sites
That is indeed very weird. All I can say is that based upon what you have posted that should work as far as I can see.

I'm struggling to know what to suggest here. All I can suggest is that you make a minimal console application with VS and confirm it compiles, then examine its differences between your project.

x.h
#include <vector>class x{public:    x();    std::vector<int*> ints;};

x.cpp
#include "x.h"x::x(){    ints.push_back(new int());}

main.cpp
#include <iostream>#include "x.h"int main(){    x *p=new x();    std::cout << x->ints.size() << std::endl;    delete x;    return 0;}

See if that works. If not, your compiler is screwed. If it does, try to find out what the difference between your code and the above minimal project is.

Sorry can't be more constructive.

##### Share on other sites
Thanks. I'll try that when I get home from work as I don't have my compiler available at the moment.

A

##### Share on other sites
Quote:
 See if that works. If not, your compiler is screwed.

its (almost*) never the compilers fault... and certainly not because your using VS 2005, there'd be a whole lot more complaints if that was the case. My advice would be to just rewrite the source files, as the problem would be something simple and ubiquitous (not an inherent compiler error). By rewriting you'll likely get it right without trying, and the plus is you always do code better the second time round. The alternative is searching for an extended amount of time to fix a basically simple problem... a final thought would be to check where your defining the cWarhead class, it could be causing the problem and also try just declaring a vector<cWarhead> in nationclass.cpp not as a member but as a local and see if that compiles. Hope that helps, its not the compiler, you just have to keep trying different things till you figure out what you did wrong. (and you did ;) )

##### Share on other sites
I wasn't suggesting there is a fundamental error with VS, merely that if the minimal project I suggested won't compile, there is something amiss with Attala101's installation.

Not that I think that is likely and I entirely agree with veiled eagle's senitments. I'm sure my minimal example will compile and just felt that by examining the differences between that and Attala101's own project, the answer may be gleaned without requiring a complete rewrite.

##### Share on other sites
Hey,

I'm not sure, but ive had problems with the VS 2005 intelisence not being able to detect things at times, mostly when there are templates involved so it may be a problem with VS. For example try the folowing, Get two classes which are radically different and put one of them in a project then goto main or somewhere and type ClassName:: and look at the intelisense list then change the name of the second class to the same name and past it over the first class then type ClassName:: again and youll get the same thing as before instead of the new stuff (after a while it updates).

I tried it with this...
/*class Animal{public:	Animal();	Animal(int i);	void Jump();	void Walk();	void Think();};*/class Animal{public:	Animal();	void Talk();	void Eat(int i);	void Sleep();};int main(){    // Ctrl + Space after the :: to get the list then uncomment the other animal    // and comment the current one, youll get the same list, took about six     // miniuts to update    Animal::}

In Summary: ItelliSense never works right and this is probally just another example :-(

Edit: I may be reading this wrong but from what i can tell its not a compiler error and it compiles fine but the IDE doesnt identify the variable when Attala101 is writting the code

##### Share on other sites
Quote:
 USA->Warheads... //whatever I try here gives me a compile error saying Warheads is not defined

I'd assumed from that this was an actual compiler error, but if Julian90 is correct and this is purely an Intellisense error, that would certainly clear things up.

Attala101 - could you just confirm this?

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013648
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!