Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

Problems with Pointers and class member functions

This topic is 4249 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'm using Linux compiler g++ I have a class called Circle and I have made a testprogram to access the member function which is declared as public. I created an array of Circles using Circle *Circles = new Circle[3]; I get the following error when I type Circle[0].SetPosition(0.0,0.0); /tmp/ccZXmAtd.o(.text+0x106): In function `main': : undefined reference to `Circle::SetPosition(float, float)' collect2: ld returned 1 exit status This is not supposed to happen.. can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
The code you posted looks right...
Hmmm...
Did you only right a prototype for the SetPosition function? If you didn't actually write the body of the function, then that might be causing the problem.

Share this post


Link to post
Share on other sites
Actually you just forgot the "s". Circle is the class name. Circles is the name of the array. So...

Circles[0].SetPosition(0.0,0.0);

Share this post


Link to post
Share on other sites
Quote:
Original post by Crazyfool
I may bve wrong, but poiinters use -> rather than .

So Circles[0]->SetPosition(0.0, 0.0);

might work?



class foobar
{
public:
void foo()
{
cout << "hello" << endl;
}
};
int main()
{
foobar *i = new foobar[3];
i[0].foo();
delete[] i;
}


Actually, that is perfectly legal. Arrays are actually pointers to memory. If you just make an array like this:
foobar i[3];
Then C++ will prevent you from assigning the array to a different section of the memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drunken_Monkey
Quote:
Original post by Crazyfool
I may bve wrong, but poiinters use -> rather than .

So Circles[0]->SetPosition(0.0, 0.0);

might work?


*** Source Snippet Removed ***
Actually, that is perfectly legal. Arrays are actually pointers to memory. If you just make an array like this:
foobar i[3];
Then C++ will prevent you from assigning the array to a different section of the memory.

To clear up any misunderstanding that may come about: Pointers use "->", non-pointers use ".". The name of an array is a pointer:
char p[10];
// p is a pointer to a char
A statically declared array can't be reassigned:
char p[] = "howdy!";
char c[] = "hello!";
c = p; // that's a no-no!

A dynamically allocated array can be reassigned (but with danger!):
char* psz = new char[] = "ahoy!";
char* psz2 = new char[] = "hello!";
std::cout << psz << "\n" << psz2 << std::endl;
psz2 = psz;
std::cout << psz << "\n" << psz2 << std::endl;

An statically allocated pointer can be reassigned (with less danger):
char* psz = new char[] = "ahoy!";
char* psz2;
std::cout << psz << std::endl;
psz2 = psz;
std::cout << psz << "\n" << psz2 << std::endl;
delete [] psz;

So, this:
Type* t = new Type[10];
t[0].Function( );
works because: t is a pointer. t[0] is an object pointed to by a pointer. As is t[1]. And t[2]. But NOT t[10].


Golly. How's one to keep all this straight? I usually let those who know infinitely more about it than I do handle it for me. I use a std::vector or std::list or std::deque or std::map or ...

@ OP: The error you get doesn't sound like it was merely a typo. Can you post some code showing exactly where you declare, define, and call the function?

-jouley

Share this post


Link to post
Share on other sites
You have declared a function SetPosition(float, float) in Circle which is not defined anywhere, i.e. you have something like:
// in a header file (.h)
class Circle
{
public:
void SetPosition(float, float);
};
and you need to add something like:
// in a source file (.cpp)
void Circle::SetPosition(float, float)
{
// code
}
Quote:
Original post by jouley
Golly. How's one to keep all this straight? I usually let those who know infinitely more about it than I do handle it for me. I use a std::vector or std::list or std::deque or std::map or ...
QFE

Σnigma

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!