Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-12
1
Medium Priority
?
329 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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