Unresolved external symbol even when constructor is defined - c++

I have the usual unresolved external symbol problem
LNK2019 unresolved external symbol "public: void __thiscall
Aldo::AssetManager::LoadTexture(class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"
(?LoadTexture#AssetManager#Aldo##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z)
referenced in function "public: virtual void __thiscall Aldo::SplashState::Init(void)"
(?Init#SplashState#Aldo##UAEXXZ)
SFML template C:\Users\ajb266\Documents\Aldo\SFML template\SFML template\SplashState.obj 1
It seems the issue is LoadTexture is not defined, but then I have defined the LoadTexture constructor in AssetManager's cpp file. It seems SplashState could also be involved. From what I understand, the object file SplashState is created after compilation of SplashState.cpp, and then linked with the libraries using a linker. Maybe I am missing something with the linker settings in Visual Studio?
My relevant files are as follows:
AssetManager.hpp:
#pragma once
#include <map>
#include <SFML/Graphics.hpp>
namespace Aldo
{
class AssetManager {
public:
AssetManager() {}
~AssetManager() {}
void LoadTexture(std::string name, std::string fileName);
sf::Texture &GetTexture(std::string name);
void LoadFont(std::string name, std::string fileName);
sf::Font &GetFont(std::string name);
private:
std::map<std::string, sf::Texture> _textures;
std::map<std::string, sf::Font> _fonts;
};
}
AssetManager.cpp:
#include "AssetManager.hpp"
namespace Aldo {
void AssetManager::LoadTexture(std::string name, std::
string fileName) {
sf::Texture tex;
if (tex.loadFromFile(fileName)) {
this->_textures[name] = tex;
}
}
sf::Texture& AssetManager::GetTexture(std::string name) {
return this->_textures.at(name);
}
void AssetManager::LoadFont(std::string name, std::string fileName) {
sf::Font font;
if (font.loadFromFile(fileName)) {
this->_fonts[name] = font;
}
}
sf::Font& AssetManager::GetFont(std::string name) {
return this->_fonts.at(name);
}
}
SplashState.hpp:
#pragma once
#include <SFML/Graphics.hpp>
#include "state.hpp"
#include "GameLoop.hpp"
namespace Aldo {
class SplashState : public State
{
public:
SplashState(GameDataRef data);
void Init();
void HandleInput();
void Update(float dt);
void Draw(float dt);
private:
GameDataRef _data;
sf::Clock _clock;
sf::Texture _backgroundTexture;
sf::Sprite _background;
};
}
SplashState.cpp:
#include <sstream>
#include "SplashState.hpp"
#include "DEFINITIONS.hpp"
#include <iostream>
namespace Aldo
{
SplashState::SplashState(GameDataRef data) : _data(data)
{
}
void SplashState::Init() {
_data->assets.LoadTexture("Splash State Background",
SPLASH_SCENE_BACKGROUND_FILEPATH);
_background.setTexture(this->_data->assets.GetTexture("Splash State Background"));
}
void SplashState::HandleInput() {/*only handle and check whether the X button to close the window is pressed or not*/
sf::Event event;
while (_data->window.pollEvent(event))
{
if (sf::Event::Closed == event.type) {
_data->window.close();
}
}
}
void SplashState::Update(float dt) {
if (_clock.getElapsedTime().asSeconds() > SPLASH_STATE_SHOW_TIME) {
std::cout << "go to main menu" << std::endl;
}
}
void SplashState::Draw(float dt) {
_data->window.clear();
_data->window.draw(_background);
_data->window.display();
}
}
I'm using Visual Studio and c++17. I'm new to C++ so forgive me if this is too trivial.

Related

error while using std:sort on vector of objects [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a vector of Person object pointers. I am trying to use std:sort to sort the vector based on the "name" of each object. I am getting an unresolved external symbol error when I try to build and run; can anyone see where I am going wrong? error:
Error LNK2019 unresolved external symbol "public: static bool __cdecl Person::sortByName(class Person *,class Person *)" (?sortByName#Person##SA_NPAV1#0#Z) referenced in function _main Lab1b C:\Users\jayjo\source\repos\Lab1b\Lab1b\Lab1b.obj 1
error
Main cpp:
#include <iostream>
#include "Person.h"
#include "Employee.h"
#include "Customer.h"
#include <vector>
#include <algorithm>
int main()
{
vector<Person*> people;
people.push_back(new Person("Peter"));
people.push_back(new Person("John"));
people.push_back(new Person("David"));
people.push_back(new Person("Aaron"));
sort(people.begin(), people.end(), Person::sortByName);
for (int i = 0; i < people.size(); i++)
{
cout << people[i]->getName() << endl;
}
}
Person.h:
#pragma once
#ifndef Person_H
#define Person_H
using namespace std;
#include <iostream>
class Person
{
public:
Person(string);
virtual void printname();
static bool sortByName(Person* A, Person* B);
string getName();
protected:
string name;
};
#endif // !Person_H
Person.cpp:
#include "Person.h"
using namespace std;
Person::Person(string n)
{
name = n;
}
void Person::printname()
{
cout << "Name: " << name << endl;
}
string Person::getName()
{
return name;
}
static bool sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()));
}
Instead of this:
static bool sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()));
}
This:
bool Person::sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()) != 0);
}
In C++, you declare the class member function as static, but you leave the static keyword off when you define it. Also, the function needs to be defined as a class member (Person::).

Visual Studio Code can't find the class that I've declared and defined in C++

I'm getting a linker error when I separate my class declarations and function definitions into their own files. The code gets through the compiler when all the code is in one singular .cpp file, but that's not what I'm trying to accomplish. I want to know how I can separate my code into different files.
Here is all the code. This code runs completely fine.
// main.cpp
#include <string>
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal {
private:
std::string name;
int height;
int weight;
std::string color;
public:
Animal(std::string, int, int);
};
#endif
Animal::Animal(std::string name, int height, int weight) {
this -> name = name;
this -> height = height;
this -> weight = weight;
}
int main() {
Animal dog("Spotty", 20, 50);
return 0;
}
Although, since I want to separate the code, I put the class declaration into it's own file and the function definitions into their own file, and I get this.
// main.cpp
#include <string>
#include "Animal.hpp"
int main() {
Animal dog("Spotty", 20, 50);
return 0;
}
// Animal.hpp
#include <string>
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal {
private:
std::string name;
int height;
int weight;
std::string color;
public:
Animal(std::string, int, int);
};
#endif
// Animal.cpp
#include "Animal.hpp"
Animal::Animal(std::string name, int height, int weight) {
this -> name = name;
this -> height = height;
this -> weight = weight;
}
This is the error I get when I compile the second version of main.cpp.
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Animal::Animal(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int)" (??0Animal##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HH#Z) referenced in function _main
main.exe : fatal error LNK1120: 1 unresolved externals
Try giving the parameters in your constructor names.
e.g
Animal(std::string name, int h, int w);
The constructor doesnt do anything right now. Do you have the function in another file?

VTK / ITK/ QT - unresolved external symbol error (LNK2001)

On building my code with VTK 7.0 with Qt5.7 and and ITK 4.5 in Visual Studio 2013, I get the error below:
error LNK2001: unresolved external symbol "protected: virtual void __cdecl vtkVRMLSource2::SetNthOutput(int,class vtkDataObject *)" (?SetNthOutput#vtkVRMLSource2##MEAAXHPEAVvtkDataObject###Z)
The code corresponding to this file is this (`vtkVRMLSource2.cxx``):
#include "vtkVRML.h"
#include "vtkVRMLSource2.h"
#include "vtkVRMLImporter.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkActorCollection.h"
#include "vtkActor.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkAppendPolyData.h"
#include "vtkTransform.h"
#include "vtkUnsignedCharArray.h"
#include "vtkSmartPointer.h"
#include "vtkFloatArray.h"
#include "vtkDataObject.h"
#include <stdio.h>
#include <iostream>
.....
idx = 0;
while ( (actor = actors->GetNextActor()) )
{
mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
if (mapper)
{
//mapper->GetInput()->Update();
//vtkPolyData *newOutput = vtkPolyData::New();
vtkPolyData *newOutput = mapper->GetInput();
//newOutput->CopyInformation(mapper->GetInput());
this->SetNthOutput(idx, newOutput);
++idx;
newOutput->Delete();
newOutput = NULL;
}
}
And the vtkVRMLSource2.h file is:
#include "vtkAlgorithm.h"
#include "vtkDataObject.h"
class vtkVRMLSource2 : public vtkAlgorithm{
public:
int vtkTypeRevisionMacro(vtkVRMLSource2, vtkAlgorithm);
void PrintSelf(ostream& os, vtkIndent indent);
static vtkVRMLSource2 *New();
int NumberOfOutputs;
vtkSetStringMacro(FileName)
vtkGetStringMacro(FileName)
int GetNumberOfOutputs();
vtkPolyData* GetOutput(int idx);
vtkPolyData* GetOutput() { return this->GetOutput(0);}
vtkSetMacro(Color,int) // usage example: this->SetColor(1);
vtkGetMacro(Color,int)
vtkBooleanMacro(Color,int)
vtkSetMacro(Append,int) // usage example: this->SetAppend(1);
vtkGetMacro(Append,int)
vtkBooleanMacro(Append,int)
protected:
vtkVRMLSource2();
~vtkVRMLSource2();
void Execute();
void InitializeImporter();
void CopyImporterToOutputs();
char* FileName;
vtkVRMLImporter *Importer;
int Color;
int Append;
virtual void SetNthOutput(int num, vtkDataObject *output);
private:
vtkVRMLSource2(const vtkVRMLSource2&);
void operator=(const vtkVRMLSource2&);
};
I have linked all the proper VTK, ITK and Qt libraries in VS.
Could you please help me?
You need to provide definition for void SetNthOutput(int num, vtkDataObject *output) which you declared in your header file. You could do it by adding this to vtkVRMLSource2.cxx:
void vtkVRMLSource2::SetNthOutput(int num, vtkDataObject *output)
{
//code goes here
}

error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit" [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I've been going through a C++/SFML tutorial (http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition.aspx) and, having reached the end, started altering the code to try out various things and get more comfortable both with C++ and SFML.
For the menu screen, I decided to create an object for buttons. To this end I created Button.cpp and Button.h, then linked to Button.h in the MainMenu.h file. I added Button button_play as a public member of class MainMenu, however when I call a Button function (for example: button_play.ButtonInit("new-game");), I receive the error: error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?ButtonInit#Button##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "public: enum MainMenu::MenuResult __thiscall MainMenu::Show(class sf::RenderWindow &)" (?Show#MainMenu##QAE?AW4MenuResult#1#AAVRenderWindow#sf###Z)
I've done a lot of searching around this, and most of the answers I've found revolve around not implementing class member functions correctly, however as far as I can tell I am doing it correctly. I am, however, very new to C++, so it's possible that I'm just missing something.
Here's my code:
MainMenu.h
#pragma once
#include "SFML\Window.hpp"
#include "SFML\Graphics.hpp"
#include "GameObjectManager.h"
#include "Button.h"
#include <list>
class MainMenu
{
public:
MainMenu(){};
~MainMenu() {};
enum MenuResult { Nothing, Exit, Play };
const static GameObjectManager& GetGameObjectManager();
struct MenuItem
{
public:
sf::Rect<int> rect;
MenuResult action;
};
MenuResult Show(sf::RenderWindow& window);
static GameObjectManager _gameObjectManager;
Button button_play;
private:
MenuResult GetMenuResponse(sf::RenderWindow& window);
MenuResult HandleClick(int x, int y);
std::list<MenuItem> _menuItems;
};
MainMenu.cpp (this is quite long; I've only included the function that calls ButtonInit() and the function that Show() returns - if you need to see more, let me know and I can include the rest of the code for this file)
#include "stdafx.h"
#include "MainMenu.h"
#include "ServiceLocator.h"
#include "Button.h"
MainMenu::MenuResult MainMenu::Show(sf::RenderWindow& window)
{
button_play.ButtonInit("new-game");
return GetMenuResponse(window);
}
MainMenu::MenuResult MainMenu::GetMenuResponse(sf::RenderWindow& window)
{
sf::Event menuEvent;
while(42 != 43)
{
while(window.pollEvent(menuEvent))
{
if(menuEvent.type == sf::Event::MouseMoved)
{
button_play.Update(window);
}
if(menuEvent.type == sf::Event::MouseButtonPressed)
{
if(ServiceLocator::GetAudio()->IsSongPlaying())
{
ServiceLocator::GetAudio()->StopAllSounds();
}
return HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y);
}
if(menuEvent.type == sf::Event::Closed)
{
return Exit;
}
}
}
}
Button.h
#pragma once
class Button
{
public:
Button() {};
~Button() {};
void ButtonInit(std::string name);
void Update(sf::RenderWindow & rw);
};
Button.cpp
#include "StdAfx.h"
#include "Button.h"
void Button::ButtonInit(std::string name)
{
}
void Button::Update(sf::RenderWindow & rw)
{
}
stdafx.h (probably don't need to see this, but just in case)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <map>
#include <iostream>
#include <cassert>
#include <string>
Any help would be appreciated.
I assume, you have both classes in the same project.
The linker-message tells you, that the linker does not find a fitting function definition.
So my guess would be ... the linker cannot find a fitting overload of the function. "new-game" is a const char* and it is not a std::string.
a) change your method signature to
void ButtonInit(const char* name);
or
b) call your method like:
button_play.ButtonInit(std::string("new-game"));

Error 21 error LNK2019: unresolved external symbol:.... referenced in function:....

I tried to create a .h file starting to a .cpp flie. I'm quite sure that the header is correct, but when i try to use the functions of the .cpp file in an other of my project I have a lot of link problems. So I attached the files here, surely someone can give me some solutions. The first solution of check the properties of the project, properties->linker->additional libraries, I have already done.
there are the .cpp files:
IfacomAmqSender.cpp
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
class IfacomAmqSender : public ExceptionListener{
private:
CountDownLatch m_latch;
CountDownLatch m_doneLatch;
Connection* m_connection;
Session* m_session;
Destination* m_destination;
MessageConsumer* m_consumer;
MessageProducer* m_producer;
std::auto_ptr<TextMessage> m_message;
long m_waitMillis;
bool m_useTopic;
bool m_sessionTransacted;
std::string m_brokerURI;
std::string m_destName;
DeliveryMode m_message_delivery_mode;
int m_message_priority;
private:
//IfacomAmqSender(const IfacomAmqSender&);
//IfacomAmqSender& operator=(const IfacomAmqSender&);
public:
IfacomAmqSender(const std::string& brokerURI, int numMessages, bool useTopic = false, bool sessionTransacted = false, const std::string& destName = "IFACOM-CMS", int waitMillis = 1000) :
m_latch(1),
m_doneLatch(numMessages),
m_connection(NULL),
m_session(NULL),
m_destination(NULL),
m_consumer(NULL),
m_waitMillis(waitMillis),
m_useTopic(useTopic),
m_sessionTransacted(sessionTransacted),
m_destName(destName),
m_brokerURI(brokerURI) {
}
IfacomAmqSender(const std::string& brokerURI, bool useTopic = false, bool sessionTransacted = false, const std::string& destName = "IFACOM-CMS") :
m_latch(1),
m_doneLatch(1),
m_connection(NULL),
m_session(NULL),
m_destination(NULL),
m_consumer(NULL),
m_waitMillis(1000),
m_useTopic(useTopic),
m_sessionTransacted(sessionTransacted),
m_destName(destName),
m_brokerURI(brokerURI) {
}
virtual ~IfacomAmqSender() {
cleanup();
}
void close() {
this->cleanup();
}
void waitUntilReady() {
m_latch.await();
}
//------ Init connexion ---------------
void createConnection()
{
// Create a ConnectionFactory
auto_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory(m_brokerURI));
// Create a Connection
m_connection = connectionFactory->createConnection();
m_connection->start();
m_connection->setExceptionListener(this);
}
void createSession()
{
// Create a Session
if (this->m_sessionTransacted == true) {
m_session = m_connection->createSession(Session::SESSION_TRANSACTED);
} else {
m_session = m_connection->createSession(Session::AUTO_ACKNOWLEDGE);
}
}
void createDestination()
{
// Create the destination (Topic or Queue)
if (m_useTopic) {
m_destination = m_session->createTopic(m_destName);
} else {
m_destination = m_session->createQueue(m_destName);
}
}
void createProducer()
{
m_producer = m_session->createProducer(m_destination);
m_producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
}
void initConnection() {
try {
createConnection();
// Create the session
createSession();
// Create the destination (Topic or Queue)
createDestination();
// Create a MessageProducer from the Session to the Topic or Queue
createProducer();
m_producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
// Indicate we are ready for messages.
m_latch.countDown();
// Wait while asynchronous messages come in.
m_doneLatch.await(m_waitMillis);
} catch (CMSException& e) {
// Indicate we are ready for messages.
//latch.countDown();
e.printStackTrace();
}
}
virtual void sendMessage(string text) {
try {
std::auto_ptr<TextMessage> message(m_session->createTextMessage(text));
// to set a property
////message->setIntProperty("Integer", ix);
m_producer->send(message.get());
message->setCMSTimestamp(System::currentTimeMillis());
} catch (CMSException& e) {
e.printStackTrace();
}
}
// Send a ActiveMQ Message
virtual void sendMessage(std::auto_ptr<TextMessage> amq_message) {
try {
amq_message->setCMSTimestamp(System::currentTimeMillis());
m_producer->send(amq_message.get());
} catch (CMSException& e) {
e.printStackTrace();
}
}
//--------------------------------------------------
// If something bad happens you see it here as this class is also been
// registered as an ExceptionListener with the connection.
virtual void onException(const CMSException& ex AMQCPP_UNUSED) {
printf("CMS Exception occurred. Shutting down client.\n");
ex.printStackTrace();
exit(1);
}
// Message Priority (0:Lowest - 9:Highest)
void setPriority(int priority){m_message_priority = priority;}
int getPriority(){return m_message_priority;}
// Message Delivery Mode
void setDeliveryMode(DeliveryMode delivery_mode){m_message_delivery_mode = delivery_mode;}
DeliveryMode getDeliveryMode(){return m_message_delivery_mode;}
Session* getSession()
{
return m_session;
}
private:
void cleanup() {
if (m_connection != NULL) {
try {
m_connection->close();
} catch (cms::CMSException& ex) {
ex.printStackTrace();
}
}
// Destroy resources.
try {
delete m_destination;
m_destination = NULL;
delete m_consumer;
m_consumer = NULL;
delete m_session;
m_session = NULL;
delete m_connection;
m_connection = NULL;
} catch (CMSException& e) {
e.printStackTrace();
}
}
};
mainwindow.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "IfacomAmqSender.h"
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
#include <qstring.h>
#include <QTextStream>
#include <QMessageBox>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
activemq::library::ActiveMQCPP::initializeLibrary();
{
std::string brokerURI;
brokerURI = "failover://(tcp://localhost:61613?wireFormat=stomp)"; // localhost
// brokerURI = "failover://(tcp://localhost:61616)"; // localhost
// Queue name
std::string destName = "IFACOM-CMS";
// Queue or Topic
bool useTopics = false; // true=Topic, false=Queue
// SESSION_TRANSACTED or AUTO_ACKNOWLEDGE
bool sessionTransacted = false; // if true, commit all messages
long long startTime = System::currentTimeMillis();
// ***** Initialisation **************************************************************
IfacomAmqSender IfacomMessageBroker(brokerURI, useTopics, sessionTransacted, destName);
IfacomMessageBroker.initConnection();
//****** Send message ******************************************************
std::string text = "My IFaCOM message";
// Customized message
try{
std::auto_ptr<TextMessage> message(IfacomMessageBroker.getSession()->createTextMessage(text));
message->setCMSTimestamp(System::currentTimeMillis());
message->setStringProperty("MyProperty", "test");
IfacomMessageBroker.sendMessage(message);
} catch (CMSException& e) {
e.printStackTrace();
}
// Simple text message
IfacomMessageBroker.sendMessage(text);
long long endTime = System::currentTimeMillis();
double totalTime = (double)(endTime - startTime) / 1000.0;
// Close the connection
IfacomMessageBroker.close();
ui->label->setText(QString::fromStdString(text));
}
// To Do at the end
activemq::library::ActiveMQCPP::shutdownLibrary();
}
main.cpp file:
#include "ifacomamqsender.h"
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
there are the header files:
IfacomAmqSender.h file:
#ifndef _IfacomAmqSender_h
#define _IfacomAmqSender_h
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
class IfacomAmqSender{
private:
CountDownLatch m_latch;
CountDownLatch m_doneLatch;
Connection* m_connection;
Session* m_session;
Destination* m_destination;
MessageConsumer* m_consumer;
MessageProducer* m_producer;
std::auto_ptr<TextMessage> m_message;
long m_waitMillis;
bool m_useTopic;
bool m_sessionTransacted;
std::string m_brokerURI;
std::string m_destName;
DeliveryMode m_message_delivery_mode;
int m_message_priority;
IfacomAmqSender(const IfacomAmqSender&);
IfacomAmqSender& operator=(const IfacomAmqSender&);
public:
IfacomAmqSender(const std::string&, int, bool, bool, const std::string&, int);
IfacomAmqSender(const std::string&, bool, bool, const std::string&);
virtual ~IfacomAmqSender();
void close();
void waitUntilReady();
void cleanup();
// KH
void createConnection();
void createSession();
void createDestination();
void createProducer();
void initConnection();
virtual void sendMessage(string);
// Send a ActiveMQ Message
virtual void sendMessage(std::auto_ptr<TextMessage>);
//--------------------------------------------------
// If something bad happens you see it here as this class is also been
// registered as an ExceptionListener with the connection.
virtual void onException(const CMSException&) ;
// Message Priority (0:Lowest - 9:Highest)
void setPriority(int);
int getPriority();
// Message Delivery Mode
void setDeliveryMode(DeliveryMode);
DeliveryMode getDeliveryMode();
Session* getSession();
};
#endif
mainwindow.h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
THE ERRORS ARE:
Error 21 error LNK2019: unresolved external symbol "public: __thiscall IfacomAmqSender::IfacomAmqSender(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool,bool,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0IfacomAmqSender##QAE#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N10#Z) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked#MainWindow##AAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 22 error LNK2019: unresolved external symbol "public: virtual __thiscall IfacomAmqSender::~IfacomAmqSender(void)" (??1IfacomAmqSender##UAE#XZ) referenced in function __catch$?on_pushButton_clicked#MainWindow##AAEXXZ$0 C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 23 error LNK2019: unresolved external symbol "public: void __thiscall IfacomAmqSender::close(void)" (?close#IfacomAmqSender##QAEXXZ) referenced in function __catch$?on_pushButton_clicked#MainWindow##AAEXXZ$0 C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 24 error LNK2019: unresolved external symbol "public: void __thiscall IfacomAmqSender::initConnection(void)" (?initConnection#IfacomAmqSender##QAEXXZ) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked#MainWindow##AAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 25 error LNK2019: unresolved external symbol "public: virtual void __thiscall IfacomAmqSender::sendMessage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?sendMessage#IfacomAmqSender##UAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function __catch$?on_pushButton_clicked#MainWindow##AAEXXZ$0 C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 26 error LNK2019: unresolved external symbol "public: virtual void __thiscall IfacomAmqSender::sendMessage(class std::auto_ptr<class cms::TextMessage>)" (?sendMessage#IfacomAmqSender##UAEXV?$auto_ptr#VTextMessage#cms###std###Z) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked#MainWindow##AAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 27 error LNK2019: unresolved external symbol "public: class cms::Session * __thiscall IfacomAmqSender::getSession(void)" (?getSession#IfacomAmqSender##QAEPAVSession#cms##XZ) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked#MainWindow##AAEXXZ) C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\mainwindow.obj GUI-CMS
Error 28 error LNK1120: 7 unresolved externals C:\Users\Marco\Desktop\Activemq\ReleaseVersions\GUI-CMS\debug\\GUI-CMS.exe GUI-CMS
I do not understand your code. it has two different definitions of class IfacomAmqSender. The first one is
class IfacomAmqSender : public ExceptionListener{
and the second one is
class IfacomAmqSender{
I think the reason of the errors is these duplications of class IfacomAmqSender
Your IfacomAmqSender constructor is declared as follows:
IfacomAmqSender(const std::string&, int, bool, bool, const std::string&, int);
The implementation header is as follows:
IfacomAmqSender(const std::string& brokerURI, int numMessages, bool useTopic = false, bool sessionTransacted = false, const std::string& destName = "IFACOM-CMS", int waitMillis = 1000);
So your implementation declares standard parameter values, while the declaration doesn't. So it's quite random whether at the place of any call, it is known that the function can take standard arguments. Either remove all standard arguments or make sure you declare them the same everywhere, both in declaration and implementation. (My C++ is a bit rusted, I think the main place for it would be the declaration in the .h file).
Oh, and hell, yes: See the other answer. The implementation has another class header around it. So you're actually declaring two classes with the same name. The CPP file usually does not have any class X : public Y declarations in it.