First c++ file and header LNK2019 error - c++

This is my first time having separate files and first time writing a header file, however I keep getting the same error I can't fix. Here are the files:
//main.cpp
#include <iostream>
#include "Bike.h"
/*
class Bike{
public:
int tyreDiameter;
int getTyreDi(){
return tyreDiameter;
}
}; */
int main(){
Bike b;
b.tyreDiameter = 50;
std::cout << b.getTyreDi();
while (1){
continue;
}
return 0;
}
//Bike.cpp
class Bike{
public:
int tyreDiameter;
int getTyreDi(void){
return tyreDiameter;
}
};
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
Now if I have only one file and use the class that is commented out in main.cpp everything works fine. But as soon as I try to separate the Bike class into another cpp file I get this error:
Error 1 error LNK2019: unresolved external symbol "public: int
__thiscall Bike::getTyreDi(void)" (?getTyreDi#Bike##QAEHXZ)
Error 2 error LNK1120: 1 unresolved externals
I am using Microsoft Visual Studio 2013.
Any help would be much appreciated

Why are you defining class Bike twice? in the cpp and in the h, the correct way would be this:
header
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
cpp
//Bike.cpp
#include "Bike.h"
int Bike::getTyreDi(void)
{
//implementation like return tyreDiameter;
}

Related

VS2015 C++ LNK2019 error when using shared enum in separate header file

Basically I want to share an enum between different classes. Separating the enum in its own header file with an unnamed namespace works fine, as long as one of the classes doesn't use the enum as a return type for a function.
The following example will not compile, it will result in a compiler error:
Test.obj : error LNK2019: unresolved external symbol "public: enum `anonymous namespace'::SharedEnum __thiscall ExampleClass::getEnum(void)" (?getEnum#ExampleClass##QAE?AW4SharedEnum#?A0xe6c5ebe7##XZ) referenced in function _main
As soon as you don't return the SharedEnum type from ExampleClass, but just an integer for example, everything compiles fine. I'd love to understand why this happens and what the best approach would be to share enums or variables between classes, without running into linker errors.
Any help is greatly appreciated!
Test.cpp
#include "stdafx.h"
#include "SharedEnum.h"
#include "ExampleClass.h"
SharedEnum mainEnum;
int main()
{
mainEnum = VALUE_1;
ExampleClass* exampleClass = new ExampleClass();
SharedEnum exampleEnum = exampleClass->getEnum();
return 0;
}
SharedEnum.h
#pragma once
namespace {
enum SharedEnum {
VALUE_1,
VALUE_2,
VALUE_3
};
}
ExampleClass.h
#pragma once
#include "SharedEnum.h"
class ExampleClass
{
public:
ExampleClass();
~ExampleClass();
SharedEnum getEnum();
private:
SharedEnum myEnum;
};
ExampleClass.cpp
#include "stdafx.h"
#include "ExampleClass.h"
ExampleClass::ExampleClass()
{
myEnum = VALUE_2;
}
ExampleClass::~ExampleClass()
{
}
SharedEnum ExampleClass::getEnum()
{
return myEnum;
}

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?

Linker error LNK2019 and LNK1120 [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
I know this problem is well known, but none of the solutions work for me. I know a popular cause of this error is the compiler can't find the definition of a function in any of the source files, but I have defined the function them.
I am using Visual studio 2015 community.
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
Form.cpp
#include "stdafx.h"
#include "Form.h"
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << endl;
}
Main.cpp
#include "stdafx.h"
#include "Form.h"
int main()
{
Form<int> f;
f.GenerateForm(12);
return 0;
}
Error:
PrimeForm.obj : error LNK2019: unresolved external symbol "public: void __thiscall Formula<double>::GenerateForm(int)" (?GenerateForm#?$Formula#N##QAEXH#Z) referenced in function _main
C:\Users\John\Dropbox\Visual Studio 2015\PrimeForm\Debug\PrimeForm.exe : fatal error LNK1120: 1 unresolved externals
When you try to compile form.cpp the compiler doesn't know what type T will be. Therefore it won't be able to compile this as an object file to be linked with your compiled main.cpp object file.
You'll need to include all of the declarations and definitions of a templated class to the files that need it (in this case your main.cpp file).
This can be simply done as follows:
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
#include "Form.template" /* Note include Form.template here */
Form.template
#include "stdafx.h"
/* Don't include form.h in this file */
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << std::endl;
}
main.cpp
#include "stdafx.h"
#include "Form.h" /* including Form.h will include Form.template as well */
int main()
{
Form<int> f; /* Compiler now knows to instantiate a Form class with type Int as the template parameter */
f.GenerateForm(12);
return 0;
}
Note the main difference is that you don't include "Form.h" in Form.template but include "Form.template" at the bottom of Form.h
It is better practice to use the ".template" file ending for templated class implementation files.

C++ function failing

My c++ program fails with below reasons:
Error 1 error LNK2005: "public: void __thiscall dataHolder::addData(int)" (?addData#dataHolder##QAEXH#Z) already defined in Source.obj
Error 2 error LNK2005: "public: void __thiscall dataHolder::findUnique(void)" (?findUnique#dataHolder##QAEXXZ) already defined in Source.obj
Error 3 error LNK2005: "public: void __thiscall dataHolder::printData(void)" (?printData#dataHolder##QAEXXZ) already defined in Source.obj
Error 4 error LNK1169: one or more multiply defined symbols found
However, when I moved all definitions in once source c++ file, it worked fine.
Below is my source code:
source.cpp file:
#include "dataClass.h"
void main(){
dataHolder v1;
for(int i=1;i<=10;i++)
v1.addData(i);
for(int i=1;i<=5;i++)
v1.addData(i);
v1.printData();
v1.findUnique();
}
dataClass.h file:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
class dataHolder{
public:
void printData();
void addData(int data);
void findUnique();
int uniqueData();
private:
vector<int> dataVector;
};
void dataHolder::addData(int val){
dataVector.push_back(val);
};
void dataHolder::printData(){
vector<int>::iterator vIt;
for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
cout<< *vIt<<endl;
};
};
void dataHolder::findUnique(){
map<int ,int> dataMap;
vector<int>::iterator vIt;
map<int ,int>::iterator mIt;
for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
if(dataMap.find(*vIt)==dataMap.end())
dataMap[*vIt]=1;
else
dataMap[*vIt] = dataMap[*vIt]+1;
};
for(mIt=dataMap.begin();mIt != dataMap.end();mIt++){
if(mIt -> second == 1)
cout<<mIt->first<<" is Unique"<<endl;
};
};
please guide for the issue.
The problem is that you define the dataHolder member functions in the header file. That means that every source file including your header file will have the definition of those member functions.
You can do that, but then you have to mark them as inline, or possibly static.
What you should do is make another source file, which contains the member function definitions, and include that source file in your project so it's compiled and linked with.
Move your implementations for the class methods into a cpp file (not the header file). This way, they don't get included multiple times.
You just should move class members implementation to .cpp file. For example dataClass.cpp:
#include "dataClass.h"
void dataHolder::addData(int val){
dataVector.push_back(val);
};
void dataHolder::printData(){
vector<int>::iterator vIt;
for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
cout<< *vIt<<endl;
};
};
void dataHolder::findUnique(){
map<int ,int> dataMap;
vector<int>::iterator vIt;
map<int ,int>::iterator mIt;
for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
if(dataMap.find(*vIt)==dataMap.end())
dataMap[*vIt]=1;
else
dataMap[*vIt] = dataMap[*vIt]+1;
};
for(mIt=dataMap.begin();mIt != dataMap.end();mIt++){
if(mIt -> second == 1)
cout<<mIt->first<<" is Unique"<<endl;
};
};
You could also use header guards to ensure that your header file is loaded only once.
#ifndef _DATA_CLASS_H_
#define _DATA_CLASS_H_
#include <...>
class dataHolder {
...
}
/* Function Definitions */
#endif /* _DATA_CLASS_H_ */
See this question: C++ - header guards

Namespaces and includes generate link error

I was playing around with namespaces when I encountered a lnk2005 error. I can't figure out how to get around the error. Here's the error:
1>Source.obj : error LNK2005: "int Chart::Bars::d" (?d#Bars#Chart##3HA) already defined in Chart.obj
1>Source.obj : error LNK2005: "class foo Chart::l" (?l#Chart##3Vfoo##A) already defined in Chart.obj
1>Source.obj : error LNK2005: "int Chart::t" (?t#Chart##3HA) already defined in Chart.obj
1>C:\Users\bnm\dev\examples\play\nmspca\Debug\nmspca.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.49
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here's the code...
Chart.h
#pragma once
#include "foo.h"
namespace Chart
{
int t;
foo l;
namespace Bars
{
int d;
}
}
Foo.h
#pragma once
class foo
{
public:
int ss;
char* h;
};
Chart.cpp
#include "Chart.h"
using namespace Chart;
int main ()
{
l.h = "g";
}
Source.cpp
#include "Chart.h"
using namespace Chart;
int test()
{
l.ss = 0;
return l.ss;
}
When the #include "Chart.h" from Source.cpp is removed the problems goes away. However, Source.cpp needs #include "Chart.h" for the namespace definition.
What's the correct way to express that "namespace Chart" is needed in both Chart.cpp and Source.cpp so that everything compiles?
If you define any objects in a header file and include that header file in multiple translation units, those objects are now defined multiple times. This is the problem you're having. The declarations of t, l, and d introduce objects and you have done so in a header file.
The proper method for supporting namespace scope variables is to declare them as extern in the header file. This makes them declarations only and not definitions. Then, in a single implementation file, define them.
Change Chart.h to:
#pragma once
#include "foo.h"
namespace Chart
{
extern int t;
extern foo l;
namespace Bars
{
extern int d;
}
}
Then in an implementation file, perhaps Chart.cpp, do:
int Chart::t;
foo Chart::t;
int Chart::Bars::d;
Everywhere you're including Chart.h, you're effectively dropping variables t, l, and d into those objects. Declare them as extern, then define them in Chart.cpp
I've had the exact same problem and I found a workarroud.
chart.h becomes:
#pragma once
#include "foo.h"
class Chart{
static int t;
static foo l;
class Bars
{
static int d;
};
};
and define the variables in chart.cpp
int Chart::t;
foo Chart::l;
int Chart::Bars::d;
Ugly, I know, but at least the syntax is the same wherever you need to use the vars.
This way only ONE .obj file is created containing the variables which prevents the multiple definition erros.
I haven't tried if you can declare a static variable on a namespace which could solve the problem.
Edit:
P.S. this solution gives some weird errors on msvc, as if every time it is included, sections of the code reference differente variables
Tested on my own program the static-inside-namespace and it seems to work.
Example.h:
namespace sdl{
static SDL_Surface* screen;
static int xres = 640;
static int yres = 480;
static int bpp = 32;
static int flags = SDL_ASYNCBLIT;
};