-
error compiling
Hello!
This is my first time making classes and I have some problems compiling and running my program.
My program consists of 3 parts:
- main.cc
- calculation.cc
- calculation.h
in the Calculation.h:
#ifndef _CALCULATION_H_
#define _CALCULATION_H_
#include <string>
using namespace std;
class Calculation {
public:
Calculation ();
virtual ~Calculation();
void getSize();
protected:
int a, b;
};
#endif
in Calculation.cc:
#include <iostream>
#include <string>
#include <vector>
#include "Calculation.h"
Calculation::Calculation() {a=0; b=0;}
Calculation::~Calculation() {}
void getSize() {
cout << "hi" << endl;
}
in the main.cc:
#include <iostream>
#include <string>
#include "Calculation.h"
using namespace std;
int main( int argc, char *argv[] ) {
Calculation calc;
calc.getSize();
}
I'm compiling and linking them this way:
g++ -c Calculation.cc
g++ -c main.cc
g++ Calculation.o main.o -o test
however, when executing "g++ Calculation.o main.o -o test", this error occurs:
Undefined first referenced
symbol in file
Calculation::getSize(void) /var/tmp/ccebkQ3K.o
ld: fatal: Symbol referencing errors. No output written to test
collect2: ld returned 1 exit status
Does anyone have any idea what this error is trying to say?
I'm so confused~~ Can anyone point me in the right direction?
Thanks a lot!!
-
in Calculation.cc, change this
>void getSize(void) {
to this
>void Calculation::getSize(void) {
And please use code tags when posting code.
-
Try:
void Calculation::getSize() {
cout << "hi" << endl;
}
You forgot to scope getSize to the class in your implementation, now it's a global function, and you are calling a member function.
-
Thanks a lot!
what are code tags ?
-
tags that contain the word 'code' :D
like this
[ c o d e ] and [ / c o d e ] (but without the spaces). It will make your code look like this:
Code:
#ifndef _CALCULATION_H_
#define _CALCULATION_H_
#include <string>
using namespace std;
class Calculation
{
public:
Calculation ();
virtual ~Calculation();
void getSize();
protected:
int a, b;
};
#endif
instead of this:
#ifndef _CALCULATION_H_
#define _CALCULATION_H_
#include <string>
using namespace std;
class Calculation
{
public:
Calculation ();
virtual ~Calculation();
void getSize();
protected:
int a, b;
};
#endif
by the way, use initialisation lists instead of assigning variables in your constructors (more efficient), and specify void as the argument list if you dont intend on passing arguments, it makes your cod easier to read:
Code:
Calculation::Calculation(void)
: a(0),
b(0)
{}
hope this helps!
U.