Solved

C++ linker unresolved symbols (splitting into *.h and *.cpp files)

Posted on 2013-11-12
1
328 Views
Last Modified: 2013-11-12
This sample program worked fine when it was all in one file. when I tried to split it into *.h and *.cpp files I'm getting linker errors now.
Error   1       error LNK2001: unresolved external symbol "public: virtual void __thiscall BaseState::eventA(class Machine *)" (?eventA@BaseState@@UAEXPAVMachine@@@Z)  C:\Users\Public\0SBCS\VS\test\StatePattern3\StatePattern3\BaseState.obj StatePattern3
BaseState.h
#pragma once

class Machine;  //forward declaration

class BaseState
{
public:
  BaseState(void);
  public:
    virtual void eventA(Machine *m);
    virtual void eventB(Machine *m);

};

Open in new window

BaseState.cpp
#include "StdAfx.h"
#include <iostream>
#include "BaseState.h"
using namespace std;

BaseState::BaseState(void)
{
}

void eventA(Machine *m)
{
    cout << "\nEventA - do nothing";
}
void eventB(Machine *m)
{
    cout << "\nEventB - do nothing";
}

Open in new window

StateONE.h
#pragma once
#include "BaseState.h"

class Machine;

class StateONE : public BaseState
{
public:
    StateONE(void);
    void eventB(Machine *m);
};

Open in new window

StateONE.cpp
#include "StdAfx.h"
#include <iostream>
#include "StateONE.h"
#include "Machine.h"
using namespace std;

StateONE::StateONE(void)
{
    cout << "\n   StateONE-ctor ";
};

void StateONE::eventB(Machine *m)
{
    cout << "\n   In StateONE, handing eventB, switching to stateTwo";
    m->setCurrent(&m->stateTwo);
}

Open in new window

StateTWO.h
#pragma once
#include "BaseState.h"

class Machine;  //forward declaration

class StateTWO : public BaseState
{
public:
    StateTWO(void);
    void eventA(Machine *m);
};

Open in new window

StateTWO.cpp
#include "StdAfx.h"
#include <iostream>
#include "StateTWO.h"
#include "Machine.h"
using namespace std;

StateTWO::StateTWO(void)
{
        cout << "\n   StateTWO-ctor ";
}

void StateTWO::eventA(Machine *m)
{
    cout << "\n   In StateTWO, handling eventA. Switching to stateOn";
    m->setCurrent(&m->stateOne);
}

Open in new window

Machine.h
#pragma once
#include "BaseState.h"
#include "StateONE.h"
#include "StateTWO.h"

class Machine
{
private:
    class BaseState *currentState;
public:
    Machine(void);
    StateONE stateOne;
    StateTWO stateTwo;
    void setCurrent(BaseState *s);
    void HandleEventA();
    void HandleEventB();
};

Open in new window

Machine.cpp
#include "StdAfx.h"
#include <iostream>
#include "Machine.h"
using namespace std;


Machine::Machine(void)
{
  cout << "\nMachine Constructor";
  currentState = &stateTwo;
}

void Machine::HandleEventA()
{
  currentState->eventA(this);
}

void Machine::HandleEventB()
{
  currentState->eventB(this);
}

void Machine::setCurrent(BaseState *s)
{
    currentState = s;
}

Open in new window

Main
#include "stdafx.h"
#include "Machine.h"

int _tmain(int argc, _TCHAR* argv[])
{
  Machine fsm;

  fsm.HandleEventB();
  fsm.HandleEventA();
  fsm.HandleEventB();

	return 0;
}

Open in new window

0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39642713
You are missin gthe class' scope with the implementations in BaseState.cpp - that should be

#include "StdAfx.h"
#include <iostream>
#include "BaseState.h"
using namespace std;

BaseState::BaseState(void)
{
}

void BaseState::eventA(Machine *m) // 'BaseState::' was missing
{
    cout << "\nEventA - do nothing";
}
void BaseState::eventB(Machine *m) // 'BaseState::' was missing
{
    cout << "\nEventB - do nothing";
}

Open in new window

0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question