Solved

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

Posted on 2013-11-12
1
326 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.

751 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