0

So I am completely stumped. I am getting an error undefined reference to Transition::Transition() which is the default constructor for one of my classes, but I swear I'm doing everything correctly. Here's whats going on in my driver.cpp

...other includes and namespace std
#include"state.h"
#include"transition.h"

int main(...){
    vector<State> stateVect;
    vector<Transition> transVect;
    State state;
    Transition transition;
}

So I KNOW that I am correctly linking my files, because I am not getting any errors for vector itself, its just the Transition transition causing me problems. But here's the kicker, my State state works just fine and its the same thing as my Transition transition basically.

Here is my full transition.h

#ifndef TRANSITION_H
#define TRANSITION_H

#include<string>

class Transition{
    public: 
//I know these all shouldn't be public, but that's not the point right now
        int q;
        string a; 
        int r; 
        string b; 
        string x; 

        Transition();
        Transition(int q, string a, int r, string b, string x);
        int getq();
        string geta();
        int getr();
        string getb();
        string getx();
};
#endif // TRANSITION_H

and here is my transition.cpp

using namespace std;

#include"transition.h"

//here it is!!!!!
Transition::Transition(){

}

//this is also somehow undefined
Transition::Transition(int qIn, string aIn, int rIn, string bIn, string xIn){
    q = qIn;
    a = aIn;
    r = rIn;
    b = bIn;
    x = xIn;
}

int Transition::getq(){
    return q;
}

string Transition::geta(){
    return a;
}

int Transition::getr(){
    return r;
}

string Transition::getb(){
    return b;
}

string Transition::getx(){
    return x;
}

Clearly I have defined a reference to Transition::Transition() (as well as the other constructor that's the one I was originally trying to get to work, but when I found that didn't work for some reason I just tried going with the default constructor). For comparison, here is my state.h and state.cpp which work.

state.h

#ifndef STATE_H
#define STATE_H

class State{
    public:
        int stateNum; //0 - 1,000
        bool isAccept;
        bool isReject;
        bool isStart; //can be also reject XOR accept

        State();
        State(int stateNum, bool isAccept, bool isReject, bool isStart);
        State& operator= (const State s);
        int getNum();
        void setAccept();
        void setReject();
        void setStart();
};
#endif //STATE_H

and state.cpp

using namespace std;

#include"state.h"

//works 
State::State(){

}

//also works
State::State(int stateNumIn, bool isAcceptIn, bool isRejectIn, bool isStartIn){
    stateNum = stateNumIn;
    isAccept = isAcceptIn;
    isReject = isRejectIn;
    isStart = isStartIn;
}

State& State::operator= (const State s){
    stateNum = s.stateNum;
    isAccept = s.isAccept;
    isReject = s.isReject;
    isStart = s.isStart;
    return *this;
}

int State::getNum(){
    return stateNum;
}

void State::setAccept(){
    isAccept = true;
}

void State::setReject(){
    isReject = true;
}

void State::setStart(){
    isStart = true;
}

I have no idea why both constructors work for State, but neither work for Transition. I've been looking at these files for half an hour and I see nothing wrong. Can anyone spot my error? Like I said, driver.cpp definitely knows about Transition, since vector throws no errors, its when I add the line Transition transition that it tells me I have a reference to something undefined. Same thing if I try to value constructor of Transition.

Tommy K
  • 1,759
  • 3
  • 28
  • 51
  • 5
    Are you linking `Transition.o` to your executable actually? – πάντα ῥεῖ Feb 12 '15 at 21:21
  • 1
    To make questions easier for others to read and answer, you should delete all unnecessary code. All the data members and member functions are irrelevant to the problem at hand. – Paul J. Lucas Feb 12 '15 at 21:23
  • 1
    `using namespace std;` should come *after* including your header (and your header should write `std::string` specifically). Otherwise you risk introducing silent ODR violations because tokens in the header get looked up differently by different units. – M.M Feb 12 '15 at 21:25
  • @πάνταῥεῖ BINGO I looked at my makefile for like 2 minutes I can't believe I didn't catch that, thanks – Tommy K Feb 12 '15 at 21:25

0 Answers0