0

This is the error I am keep getting . undefined reference to my class. I am not sure. I think I m linking them. this is how my main looks like.

    #include <iostream>
    #include "randomNumberMagnifier.h"

    using namespace std;

     int main()
      {
        randomNumberMagnifier r1, r2;

      cout << "Random Number "<< r1.getRandomNumber();
       cout << endl;


      } 

I am not sure what I am doing wrong. this is what it looks like. when I compile

      [singha1@cs1 p4]$ g++ -c randomNumberMagnifier.cpp
       [singha1@cs1 p4]$ g++ -o p4Driver.cpp
        g++: no input files
        p4Driver.cpp:(.text+0x8c): undefined reference to `randomNumberMagnifier::getRandomNumber
     collect2: ld returned 1 exit status



      #ifndef RANDOMNUMBERMAGNIFIER_H
#define RANDOMNUMBERMAGNIFIER_H

class randomNumberMagnifier
{
  int addFactor;
  int multFactor;
  bool addOn;
  bool multOn;
  int randomNumber;
  static const int MAX_ADD_FACTOR = 100;
  static const int MAX_MULT_FACTOR = 20;
  static const int MAX_RANDOM = 200;

 public:
  randomNumberMagnifier();

  //~randomNumberMagnifer();

  randomNumberMagnifier& operator=(const randomNumberMagnifier& rhs);

  randomNumberMagnifier(const randomNumberMagnifier& arandom);

  randomNumberMagnifier(bool aState, bool mState);

  int randomMagnifier();

  int getAdd();
  int getMult();

  bool getAddState();
  bool getMultState();

  int getRandomNumber();


};
#endif
AAA
  • 35
  • 2
  • 12

2 Answers2

3
g++ -o p4Driver.cpp

That doesn't say what it's supposed to compile to, which is what -o is supposed to be for. You want:

g++ -c randomNumberMagnifier.cpp
g++ -c p4Driver.cpp
g++ randomNumberMagnifier.o p4Driver.o -o p4Driver

Or just:

g++ randomNumberMangifier.cpp p4Driver.cpp -o p4Driver
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

you need to provide randomNumberMagnifier.o to g++ command, so it can find function definition. I tested with below command and i worked

g++ -o p4Driver p4Driver.cpp randomNumberMagnifier.o
billz
  • 44,644
  • 9
  • 83
  • 100
  • /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' randomNumberMagnifier.o: In function `randomNumberMagnifier::randomNumberMagnifier(bool, bool)': randomNumberMagnifier.cpp:(.text+0x171): undefined reference to `randomNumberMagnifier::MAX_RANDOM' randomNumberMagnifier.o: In function `randomNumberMagnifier::randomNumberMagnifier(bool, bool)': randomNumberMagnifier.cpp:(.text+0x235): undefined reference to `randomNumberMagnifier::MAX_RANDOM' collect2: ld returned 1 exit status – AAA Nov 02 '12 at 02:05
  • can't find MAX_RANDOM? is your randomNumberMagnifier.h or .cpp depend on other file and you haven't included them? – billz Nov 02 '12 at 02:11
  • did you initialise MAX_RANDOM in randomNumberMagnifier.cpp to int randomNumberMagnifier::MAX_RANDOM = 0; ? – billz Nov 02 '12 at 02:17
  • When i do this g++ -o p4Driver p4Driver.cpp randomNumberMagnifier.o how do i run the file after that? – AAA Nov 02 '12 at 02:23
  • p4Driver binary will be generated. ./p4Driver – billz Nov 02 '12 at 02:25