I know this is probably an unbelievably simple error somewhere, but I can't see it. I keep getting "undefined reference to Machine::Machine()" and all of Machine's other functions.

main.cpp

#include "machine.h"

using namespace std;

int main(int argc, char** argv) {
   Machine* m = new Machine();
   m->reset();
   m->run();
   delete m;
   return 0;
}

machine.h

#ifndef _MACHINE_H_
#define _MACHINE_H_

#include "user.h"

class Machine {
public:
   Machine();
   ~Machine();

   void run();
   void reset();

private:
   User currentUser;
};

#endif

machine.cpp

#include "machine.h"

Machine::Machine() {
}

Machine::~Machine() {
}

void Machine::run() {
}

void Machine::reset() {
}

I know I'm going to kick myself once someone answers this. Thanks in advance.
-andrax

Is machine.cpp in your project / makefile?
Do you ever see machine.cpp being compiled when main.cpp is compiled?

Doesn't get to compile at all. The error keeps it from getting that far.

Makefile

OBJS = main.o machine.o user.o
EXENAME = sucrose
C = g++
COPTS = -g -Wall
LINK = g++
LINKOPTS = -o $(EXENAME)

$(EXENAME):  $(OBJS) $(LINK) $(LINKOPTS) $(OBJS)

main.o : main.cpp machine.h
        $(C) $(COPTS) main.cpp

machine.o : machine.cpp machine.h user.h
        $(C) $(COPTS) machine.cpp

user.o : user.cpp user.h
        $(C) $(COPTS) user.cpp

clean:
        -rm *o $(EXENAME)

> $(EXENAME): $(OBJS) $(LINK) $(LINKOPTS) $(OBJS)
Shouldn't this be

$(EXENAME):  $(OBJS)
        $(LINK) $(LINKOPTS) $(OBJS)

Also, COPTS should also include the -c command line flag. This means "compile only", and is necessary to turn each file into its corresponding .o file. As written, each target tries to compile and link the whole program, which will cause the problem you see.

Does typing this at the command line also work? g++ main.cpp machine.cpp user.cpp

I made those changes and it still doesn't work. The command-line statement gave the same error too.

Thanks for waiting a whole month to post "it doesn't work" :icon_rolleyes:

Now try again and post some actual command lines, actual error messages, and the code if it's different from what you originally posted.

This article has been dead for over six months. Start a new discussion instead.