C++ help

by Ardmore

Back to Mechanic's Corner.

Ardmore2008-08-05 22:45:36
I don't know if we're suppose to post non-MUD help here but... Oh well

So, I'm a pretty big noob when it comes to pointers. I just don't understand them well.

What I'm trying to do is populate a vector of pointers. HALP.

Here's my Token file.

class Token
{
public:
virtual ~Token();
virtual std::string to_string() { return typeid(*this).name(); };
};

class Operand : public Token
{
/** DON'T FORGET TO PUT PRECIDENCE ANALYZER HERE */
};

class Integer : public Operand
{
int m_value;
public:
Integer(int value) : m_value(value) { };
std::string to_string()
{
std::ostringstream oss;
oss << typeid(*this).name() << m_value;
return oss.str().c_str();
};
};

class Real : public Operand
{
double m_value;
public:
Real(double value) : m_value(value) { };
std::string to_string()
{
std::ostringstream oss;
oss << typeid(*this).name() << m_value;
return oss.str().c_str();
};
};

And my simple push_back for the vector

typedef boost::shared_ptr TokenPtr;
TokenPtr p;
p.reset( reinterpret_cast( new Integer(5) ) );
std::vector tokens;
tokens.push_back( p );

The error (bleh, linker) I get is...

LNK2019: unresolved external symbol "public: virtual __thiscall Token::~Token(void)" (??1Token@@UAE@XZ) referenced in
function "public: virtual void * __thiscall Token::`scalar deleting destructor'(unsigned int)" (??_GToken@@UAEPAXI@Z) main.obj


HALP!
Ardmore2008-08-05 22:48:16
...nevermind, fixed it.

virtual ~Token();

should have been

virtual ~Token() { };

censor.gif
Acrune2008-08-05 22:54:32
Heh, yeah. For programming with Visual Studios, just google the error number (LNK2019 in this case) and msdn tends to have a helpful list of causes/solutions.

Unresolved eternal errors are caused when you declare a function but the compiler can't find or can't determine the right function definition to go with it when you call it.

The ~ function is a destructor, so even though you didn't explicitly call it, its called when an instance of the class goes out of scope, which is what caused the unresolved external.
Ardmore2008-08-05 22:57:32
Yeah - be great if it wouldn't let me compile at all if I have an error like that. It shouldn't wait until I call it. sad.gif
Acrune2008-08-05 22:59:37
It would (kinda, would slow down compiling I think), but once you know what causes the errors, they're typically really easy to fix.
Ardmore2008-08-05 23:02:12
:| Now I'm stuck on dereferencing the vector to get at the various pointers...

God I hate pointers
Ardmore2008-08-05 23:07:48
Last question for the... minute.

Is there any other way to do this besides

for(vector::const_iterator i = tokens.begin(); i != tokens.end(); ++i)
{
p = *i;
cout << p->to_string() << endl;
}

p is a boost::shared_ptr
I know, I know, for loops are the devil... Just using one to make sure it outputs the correct stuff.

Ardmore2008-08-05 23:11:03
...nevermind again, figured it out again

cout << i->get()->to_string() << endl;

This is embarrassing. I can do some really hard stuff, but not simple pointers.
Acrune2008-08-05 23:53:42
Heh, I remember in high school they taught me pointers, and I was like "Man, thats stupid, I'll just ignore this unit, I'll never use this"

Boy was I wrong. wtf.gif
Charune2008-08-05 23:54:21
QUOTE(Ardmore @ Aug 5 2008, 07:02 PM) 541490
God I hate pointers

Though I don't know C++ (yet), I have experience with using pointers in C. For some reason, after using them enough, I grew to like them. Give them time!
Acrune2008-08-06 00:07:22
Yeah, they don't bother me anymore.
Unknown2008-08-06 00:41:46
:: Makes a mental note that pointers = important. ::
Ardmore2008-08-06 00:50:39
I understand the basics - it's when I start using them for inheritance and storing them in containers I start getting overwhelmed.

If anyone's wondering my assignment is to build a console based calculator. I'm having problems already and I'm still on the easy part! cry.gif
Desitrus2008-08-06 01:57:40
QUOTE(Charune @ Aug 5 2008, 06:54 PM) 541509
Though I don't know C++ (yet), I have experience with using pointers in C. For some reason, after using them enough, I grew to like them. Give them time!


Still no C++? Eesh!
Unknown2008-08-06 02:23:50
I know C/C++ pretty well, so I could help you with some of the finer points.

For starters, you DID get a compilation error of sorts when you didn't implement your Token destructor. There's pre-compiling, compiling, and then linking. You just didn't have a problem until the final stage, but the error message is clear enough to let you know what's missing.

I don't mind pointers at all now, really. Templates, like the STL and boost, are much more of a pain, in my opinion.

You can do your code inside your for loop this way, too, if I'm not mistaken:
CODE
cout << (*i)->to_string() << endl;
Charune2008-08-06 02:30:10
QUOTE(Desitrus @ Aug 5 2008, 09:57 PM) 541549
Still no C++? Eesh!

I know Objective C and have done a little work with C#, so I've skirted around C++ so far. I've been meaning to learn it eventually, but I've not bothered yet!
Unknown2008-08-06 02:31:50
QUOTE(Charune @ Aug 5 2008, 10:30 PM) 541571
I know Objective C and have done a little work with C#, so I've skirted around C++ so far. I've been meaning to learn it eventually, but I've not bothered yet!


That's like...

"I can crawl, and I can run, but I can't walk for the life of me!"
Gartinua2008-08-06 02:51:39
QUOTE(Ardmore @ Aug 6 2008, 09:02 AM) 541490
God I hate pointers

Nooo, pointers are the Timtams of programming. So bad for you but oh so yummy!

Any language I use that doesn't have pointers, I get rather sad about.

Pointers! dribble.gif

I never bothered to learn C++ myself, why bother when you have C (which has lots of pointers in it). Good luck on your programming project.


Ardmore2008-08-06 02:55:42
Accelerated C++ by Andrew Koenig and Barbara E. Moo is the best beginners book I've seen for C++. It skips around all the junk you'll never use and jumps right into the important stuff.

I was impressed how it didn't mention an array but jumped right into vectors and string manipulation.

Solid read.

Night!
Acrune2008-08-06 02:59:37
QUOTE(Myrkr @ Aug 5 2008, 10:31 PM) 541572
That's like...

"I can crawl, and I can run, but I can't walk for the life of me!"


Except not really, since that implies that all three are mostly only different in difficulty. Other then C++ being the most commonly used of the three at the moment, there's nothing keeping you from learning C and C# without C++.