Hello I am having some troubles with linking header files that contains templates. I have heard that using namespace could resolve this linking issue, but I could not get it to work. Thanks in advance.
//utility.h
#ifndef _UTILITY_H_
#define _UTILITY_H_
#include<iostream>
#include<string>
#include<vector>
using namespace std;
namespace utility
{
template<typename T>
void space_b4(T &value, int &max_num_length);
template<class T>
string doub_to_str(T &d); //Converting double to string.
}
using namespace utility;
template<class T>
string doub_to_str(T &d) //Converting double to string.
{
stringstream ss;
ss << d;
return ss.str();
}
template<typename T>
void space_b4(T &value, int &max_num_length) //This function adds space before an element if the number of digits of this element is less than the maximum number.
{
int d = max_num_length - doub_to_str(value).length();
for (int a = 0; a < d / 2; a++)
{
cout << " ";
}
}
#endif
Here is my main cpp file: Data management.cpp
//Data management.cpp
#include <iostream>
#include"utility.h"
using namespace std;
using namespace utility;
int main()
{
double a;
int max;
max = 10;
utility::space_b4(a, max);
}
Here are the error messages:
1>Data management.obj : error LNK2019: unresolved external symbol "void __cdecl utility::space_b4<double>(double &,int &)" (??$space_b4#N#utility##YAXAANAAH#Z) referenced in function _main
1>C:\Users\liuxi_000\Documents\C++\Final project_test\Final Project\Debug\Final Project.exe : fatal error LNK1120: 1 unresolved externals
You declare template functions utility::space_b4 and utility::doub_to_str, but the definitions are in global namespace.
To fix this, move the definitions into the namespace utility { } block:
namespace utility
{
template<typename T>
void space_b4(T &value, int &max_num_length);
template<class T>
string doub_to_str(T &d); //Converting double to string.
}
namespace utility
{
template<class T>
string doub_to_str(T &d) //Converting double to string.
{
stringstream ss;
ss << d;
return ss.str();
}
template<typename T>
void space_b4(T &value, int &max_num_length) //This function adds space before an element if the number of digits of this element is less than the maximum number.
{
int d = max_num_length - doub_to_str(value).length();
for (int a = 0; a < d / 2; a++)
{
cout << " ";
}
}
}
Related
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'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)
I'm trying to add elements to a vector in my program. But I'm not sure if I'm doing it right. So far what I have below does not work. I get an error that says:
error LNK2019: unresolved external symbol "public: __thiscall MySet::MySet(void)" (??0MySet##QAE#XZ) referenced in function _main
Here is my code:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class MySet{
public:
vector<int> elements;
MySet();
void addElement(int value);
int removeElement(int index);
int sum();
int size();
};
void MySet::addElement(int value){
elements.push_back(value);
}
int main(int argc, char *argv[]){
int value;
MySet set;
cout << "Enter your numbers " << endl;
cin >> value;
while(value != -1){
set.addElement(value);
}
system("PAUSE");
}
first:
You never define your MySet ctor.Define it or remove your declaration of MySet().
second:
cin>>value out of while loop,so just input once,you maybe want write code like this:
EDIT:
while(cin >> value){
if(value==-1)
break;
set.addElement(value);
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I was just trying to see if I can read a text file and display but I have this error:
2 error LNK2019: unresolved external symbol "public: void __thiscall
WeatherReport::displayReport(void)"
(?displayReport#WeatherReport##QAEXXZ) referenced in function _main
Can anyone explain me what is causing this, why this is happening and how to fix this problem?
#include<fstream>
#include<iomanip>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
class WeatherReport
{
WeatherReport friend monthEnd(WeatherReport, WeatherReport);
private:
int dayofMonth;
int highTemp;
int lowTemp;
double amoutRain;
double amoutSnow;
public:
WeatherReport(int Day = 0);
void setValues(int, int, int, double, double);
void getValues();
void displayReport();
}
void WeatherReport::setValues(int dom, int ht, int lt, double ar, double as)
{
dayofMonth = dom;
highTemp = ht;
lowTemp = lt;
amoutRain = ar;
amoutSnow = as;
}
int main()
{
const int DAYS = 30;
WeatherReport day[DAYS];
WeatherReport summary;
int i = 0;
ifstream inFile;
inFile.open("WeatherTest.txt");
if (!inFile)
cout << "File not opended!" << endl;
else
{
int dom, ht, lt;
double ar, as;
while (inFile >> dom >> ht >> lt >> ar >> as)
{
day[i].setValues(dom, ht, lt, ar, as);
i++;
}
inFile.close();
for (int i = 0; i < DAYS; i++)
{
day[i].displayReport();
//read one line of data from the file
//pass the data to setValues to initialize the object
}
system("PAUSE");
return 0;
}
Your displayReport does not have a function body, and as such does not have an external symbol referencing it, hence the error.
Add a function body for displayReport, and the issue will go away:
void WeatherReport::displayReport()
{
//Place your code here.
}
The following code can be used to reproduce this error:
[header file- test.h]:
#include "StdAfx.h"
void someOtherFunction();
void someFunction(string thisVar);
[code file- test.cpp]:
#include "StdAfx.h"
#include "test.h"
void someOtherFunction()
{
printf("Hello World!");
}
[function body for someFunction(string thisVar) is missing!]
The error speaks for itself
LNK2019: unresolved external symbol "public: void __thiscall WeatherReport::displayReport(void)
It can't find the definition for WeatherReport::displayReport(). I see its declaration in your code, but there's no definition anywhere. Either you didn't write a definition, or you provided it and didn't link the .cpp file that it's in. I'm guessing the former.
Seems like displayReport() does not have a body - it is only declared, but not defined. Add the following:
void WeatherReport::displayReport()
{
//your code
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm trying to test a program and every time I go to compile it, I get the error LNK2019: unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main. I was wondering what is causing this and how I can fix it. I've tried messing around with the code, but can't figure what is causing it.
Prog3Graph.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "Prog3Graph.h"
#include "GraphNode.h"
using namespace std;
int main()
{
Prog3Graph *test;
test = new Prog3Graph();
test->buildGraph("Graph.txt");
test->printGraph();
return 0;
}
bool Prog3Graph::buildGraph(char *fileName)
{
int i,j,index,numlinks, link;
char line[24];
ifstream inFile;
inFile.open(fileName, ifstream::in);
if(!inFile.is_open())
{
cout << "Unable to open file " << fileName << ". \nProgram terminating...\n";
return 0;
}
for(i=0;i<10;i++)
{
getNextLine(line,24);
index = atoi(line);
Nodes[i].setNodeID(index);
getNextLine(line,24);
Nodes[i].setNodeData(line);
getNextLine(line,24);
numlinks = atoi(line);
for(j=0;j<numlinks;j++)
{
getNextLine(line,24);
link = atoi(line);
AdjMatrix[i][link]=1;
}
inFile.close();
}
return true;
}
void Prog3Graph::printGraph()
{
int i,j;
cout << "------------------------------------------------------------\n\n";
cout << " Adjacency Matrix:\n\n";
cout << " 0 1 2 3 4 5 6 7 8 9\n";
cout << " +---------------+\n";
for(i=0; i<10; i++)
{
cout << i << "|";
for(j=0; j<10; j++)
{
cout << AdjMatrix[i][j] << "|";
}
cout << "\n +---------------+\n";
}
}
bool Prog3Graph::getNextLine(char *line, int lineLen)
{
int done = false;
ifstream inFile;
while(!done)
{
inFile.getline(line, lineLen);
if(inFile.good())
{
if(strlen(line) == 0)
continue;
else if(line[0] == '#')
continue;
else done = true;
}
else
{
strcpy(line, "");
return false;
}
}
return true;
}
Prog3Graph.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
class Prog3Graph
{
private:
ifstream inFile; // File stream to read from
int AdjMatrix[10][10];
GraphNode Nodes[10];
public:
Prog3Graph(); // Class constructor
~Prog3Graph(); // Class destructor
bool buildGraph(char *filename); // Read graph file, build graph
void printGraph(); // Print all data in graph
void depthFirstTraversal(); // Perform a depth first traversal
private:
bool getNextLine(char *line, int lineLen); // Read next line from graph file
};
GraphNode.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
void GraphNode::setNodeID(int ID)
{
m_iNodeID = ID;
}
int GraphNode::getNodeID()
{
return m_iNodeID;
}
void GraphNode::setNodeData(char *data)
{
int i;
for(i=0;i<24;i++)
{
m_sNodeData[i] = data[i];
}
}
char *GraphNode::getNodeData()
{
return &m_sNodeData[24];
}
void GraphNode::setVisited(bool visited)
{
m_bVisited = visited;
}
bool GraphNode::hasBeenVisited()
{
return m_bVisited;
}
GraphNode.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class GraphNode
{
private:
int m_iNodeID;
char m_sNodeData[24];
bool m_bVisited;
public:
GraphNode();
~GraphNode();
void setNodeID(int ID);
int getNodeID();
void setNodeData(char *data);
char *getNodeData();
void setVisited(bool visited);
bool hasBeenVisited();
};
Read the message carefully:
unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main.
You've declared a constructor (and a destructor) for your class, but you've never actually defined them.
Im receiving this error when trying to compile my code.
$ g++ -o BangBangControlTest BangBangControl.o BangBangControlTest.o
ld: duplicate symbol _heating_unit in BangBangControlTest.o and BangBangControl.o for architecture x86_64
collect2: ld returned 1 exit status
I am new to C++ and can't find out what is wrong. I've searched through many tutorials and looked at similar error messages received by other stack users. Here are my classes.
"BangBangControlTest.cpp"
// Test function
#include <iostream>
#include "BangBangControl.h"
using namespace std;
int main(){
BangBangControl control(50, true, 75);
for(int i = 0; i < 50; i++){
std::cout << "Temp = " << control.update() << endl;
}
return 0;
}
"BangBangControl.cpp"
#include <iostream>
#include "BangBangControl.h"
using namespace std;
BangBangControl::BangBangControl(int temp, bool isOn, int initialTemp){
heating_unit = HeatingUnit(isOn, initialTemp);
temp_to_maintain = temp;
}
void BangBangControl::setTemp(int temp){temp_to_maintain = temp;}
int BangBangControl::getTemp(){return temp_to_maintain;}
int BangBangControl::update(){
int b=heating_unit.tick();
if (b > temp_to_maintain + 2) heating_unit.turnOff(); if (b < temp_to_maintain - 2) heating_unit.turnOn();
return b;
}
"BangBangControl.h"
// BangBangControl header
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit heating_unit;
int temp_to_maintain;
class BangBangControl{
public:
BangBangControl(int, bool, int);
void setTemp(int);
int getTemp();
int update();
};
"HeatingUnit.cpp"
// HeatingUnit class implementation
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit::HeatingUnit(bool a, int b){
isOn = a;
temp = b;
}
void HeatingUnit::turnOn(){isOn = true;}
void HeatingUnit::turnOff(){isOn = false;}
int HeatingUnit::tick(){
if(isOn && temp <= 100){
return ++temp;
}
else if((!isOn) && temp >= 0){
return --temp;
}
else{
return temp;
}
}
"HeatingUnit.h"
#include <iostream>
using namespace std;
class HeatingUnit{
public:
bool isOn;
int temp;
HeatingUnit();
HeatingUnit(bool, int);
void turnOn();
void turnOff();
int tick();
};
You see that HeatingUnit heating_unit; in your header file? You need to put extern in front of it, and copy the original version without the extern to the .cpp file, optionally specifying an initial value there.
You can read more about this here: How do I use extern to share variables between source files?