Jump to content
  • Advertisement
Sign in to follow this  
Attala101

std::vector problem

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

If you intended to correct an error in the post then please contact us.

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:

	cWarhead( );
	~cWarhead();

		int DamagePotential;

		enum eTypeOfWarhead { LCB, MCB, BCB, LNB, MNB, BNB }TypeOfWarhead;
	
};

class nationclass
{
public:

	nationclass( );
	~nationclass();

		std::string sCountryName, sCityName[3];

	int targetcountry, targetcity;

	std::vector<cWarhead*> Warheads;

	
	
};

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Thanks for your reply, and your tip about a vector of pointers, I will change that later.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!