error compiling

This is a discussion on error compiling within the C++ Programming forums, part of the General Programming Boards category; Hello! This is my first time making classes and I have some problems compiling and running my program. My program ...

  1. #1
    Unregistered
    Guest

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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    in Calculation.cc, change this
    >void getSize(void) {

    to this

    >void Calculation::getSize(void) {

    And please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    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.

  4. #4
    Unregistered
    Guest
    Thanks a lot!

    what are code tags ?

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    tags that contain the word 'code'

    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21