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.


#include "machine.h"

using namespace std;

int main(int argc, char** argv) {
   Machine* m = new Machine();
   delete m;
   return 0;


#ifndef _MACHINE_H_
#define _MACHINE_H_

#include "user.h"

class Machine {

   void run();
   void reset();

   User currentUser;



#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.

Re: dumb class scoping error 80 80

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

Re: dumb class scoping error 80 80

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


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


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

        -rm *o $(EXENAME)
Re: dumb class scoping error 80 80

Shouldn't this be

        $(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

Re: dumb class scoping error 80 80

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

Re: dumb class scoping error 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.