Problems with Pointers and class member functions

Started by
5 comments, last by Enigma 17 years, 2 months ago
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?
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
I may bve wrong, but poiinters use -> rather than .

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

might work?
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.
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);

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.
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
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

This topic is closed to new replies.

Advertisement