GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Apr-2009, 00:31
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Need some help with undefined reference compile error


Hi, I've just recently started my new practical for uni, but when I go to compile my code, I get an error saying that:

Code:
/cygdrive/c/users/[myname]/appdata/local/temp/ccf57Lc.o:hp-35.cpp:(.text+0x2be):undefined reference to 'HPstack::pop()'

I have been searching on the internet for the past hour or so trying to find a fix, but have had no luck. Can anyone help with how I can fix this?
My code is as follows:

hp-35.cpp - I didn't write this code, it was written by our lecturer, I have only added the includes and namespace stuff (he gave us the main function, it just wasn't wrapped in all the normal stuff for the class).
CPP / C++ / C Code:
#include<iostream>
#include <sstream>
#include "HPStack.h"

using namespace std;

int main() {
	HPStack stack;
	string line;
	
	while (getline(cin, line)) {
		stringstream expression(line);
		string token;
		while (expression >> token) {
			if (isdigit(token[0])) {
				stack.push(atof(token.data()));
			} else if (token == "+") { // other arithmetic ops similar
				double x = stack.pop();
				double y = stack.pop();
				stack.push(y + x);
			}
		}
		cout << stack.peek();
	}
	return 0;
}
HPStack.h
CPP / C++ / C Code:
#ifndef HPSTACK_H_
#define HPSTACK_H_

class HPStack {

	public:
		HPStack();
		void push(double d);
		double pop();
		double peek();

};
#endif
HPStack.cpp
CPP / C++ / C Code:
#include<iostream>
#include "HPStack.h"

using namespace std;

double reg[4];

int main() {
	
	

	return 0;
}


void HPStack::push(double d) {					//shift all elements right one place in array and store new value into X register

	for(int i = 4 ; i > 0 ; i--){
		reg[i] = reg[i-1];
	}
	reg[0] = d;
}

double HPStack::pop(){								//shift all elements left in array and return old X register value
	double temp = reg[1];
	for(int i = 0 ; i < 4 ; i++){
		reg[i] = reg[i+1];
	}
	return temp;
}

double HPStack::peek(){								//display current value stored in X register
	return reg[0];
}
  #2  
Old 15-Apr-2009, 05:15
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Need some help with undefined reference compile error


you also need to compile HPStack.cpp. If you are only compiling hp-35.cpp then the definitions of HPStack are not being built and the linker is unable to find them.
What is the command you are using to compile?
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 15-Apr-2009, 09:19
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Need some help with undefined reference compile error


Oh... I was merely typing
Code:
g++ -o hp-35 hp-35.cpp
into cygwin..
So it doesn't matter that I compile HPStack.cpp first, and then compile hp-35.cpp after? The linker still won't be able to find them?

How would I go about compiling two at the one time?
  #4  
Old 15-Apr-2009, 09:50
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Need some help with undefined reference compile error


I have just found on another website about including HPStack.cpp in my hp-35.cpp file by doing
Code:
#include "HPStack.cpp"
and I also removed the main function in HPStack.cpp so now instead of it being
CPP / C++ / C Code:
int main(){...}
it is
CPP / C++ / C Code:
HPStack::HPStack(){

}

Does this seem valid? It appears to work, but I'm not sure if it's going to be limited use or not.

Thanks
  #5  
Old 15-Apr-2009, 12:35
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Need some help with undefined reference compile error


Thanks for the reply dave.

With the command line you gave, could you please give a quick explanation as to what the -Wall -W and -pedantic stand for?

Other than that it looks pretty simple how to fix the problem .
Thanks
  #6  
Old 15-Apr-2009, 13:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need some help with undefined reference compile error


Quote:
Originally Posted by Denno
Thanks for the reply dave.
Ummm... Oops: Due to an unfortunate "editing accident," I managed to get the response out of order.

For the benefit of others, here's what I said (more-or-less)
Quote:
Originally Posted by davekw7x
Quote:
Originally Posted by Denno
...How would I go about compiling two at the one time?

Put all source files on the command line (it doesn't matter which order)

For example

Code:
g++ -Wall -W -pedantic hp-35.cpp HPStack.cpp -o hp-35



Quote:
Originally Posted by Denno
another website about including HPStack.cpp in my hp-35.cpp file...

I think it is a bad habit to get into. It would (probably) work for your simple project, but there are lots of reasons not to do this.

When your projects get a little more "interesting" (files compiled at different times and some of them include some common cpp files) it simply won't work.

So: I strongly recommend that you learn to do it the 'right' way (don't include any files that declare variables or that contain instructions that generate executable code), and avoid confusion later.

Now, back to the present:

Quote:
Originally Posted by Denno
With the command line you gave, could you please give a quick explanation as to what the -Wall -W and -pedantic stand for?
These command-line switches turn on compiler warnings. The GNU guys put a lot of effort in trying to let their compiler spot various "typical" problems that are not, strictly speaking, violations of the language specification but may very well be real problems in getting the program to work. See Footnote. For me, the warning switches on the command are an idiom. I always use them without thinking about whether I need them or not. (Kind of like including <iostream>, it's automatic. No thought required.)

After you get more experienced, you may decide to turn off some of the warning flags or to ignore certain types of warnings, but for now, I recommend that you let the compiler help you as much as it can. In other words, turn on the warning switches and make whatever changes you need in order to get a "clean" compile: no warnings, no errors.


Now, back to your code;

Compiling your two C++ files together into a single program won't work, since you have, for some reason, put a main() function in each of them. Programs like this always have exactly one main() function. Lose the entire main() function in HPStack.cpp and try my command line for compiling.

I think that you will still get the error message something like
Code:
hp-35.cpp:(.text+0x8e): undefined reference to `HPStack::HPStack()'

In the header file you have declared a constructor:
CPP / C++ / C Code:
.
.
.
  public:
    HPStack();
.
.
.

Now, if you don't declare a constructor, the compiler will helpfully supply a default, but if you declare a constructor, then you simply must supply an implementation.

Plan 1: If you don't want your constructor to do anything "special," you can just comment out the HPStack() line in the header.

Plan 2: Since you might someday want to make the constructor do something interesting, you supply "placeholder" code for a constructor, but give it an empty block. That is, leave the header file as is and in HPStack.cpp, you can add a line or two something like:
CPP / C++ / C Code:
// Constructor: For now, it doesn't actually do anything constructive
HPStack::HPStack() {}

Now try the compile stuff again.

Regards,

Dave

Footnote: For a reference on compiler warnings, and to see what kind of probably-unfortunate-but-not-strictly-illegal code gets flagged by these warnings, see http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  #7  
Old 15-Apr-2009, 19:34
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Need some help with undefined reference compile error


Thankyou very much dave! I have used the command line compile code you suggested, and I already have the empty constructor from post #4 in this thread.
I compiles the same as it was before, although I'm sure this is a must more efficient way for it to work.
Thanks again .
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Linked Lists advice request promsan C Programming Language 74 23-May-2007 08:29
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 10:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:05.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.