"unresolved external..." when trying to build a class with CPP - c++

Today I downloaded the new version of visual studio, and I tried to build a class:
class Fraction
{
public:
Fraction(int, int);
private:
int _a;
int _b;
};
I also built a cpp file for the implementation:
#pragma once
#include "Fraction.h"
#include <string>
Fraction::Fraction(int a, int b)
{
_a = a;
_b = b;
}
The header file (thc class) is in file called "Franctions.h" and found in the header files directory.
The cpp file (thc class) is in file called "Franctions.cpp" and found in the Resource files directory.
When Im trying to run this simple program, I get this issue:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall Fraction::Fraction(int,int)" (??0Fraction##QAE#HH#Z) referenced in function _main Homework2 c:\Users\VVV\documents\visual studio 2017\Projects\Homework2\Homework2\questionA.obj 1
Why can it be happening?
EDIT:
main:
#include "Fraction.h"
int main()
{
Fraction f1(1, 2);
return 0;
}

The problem is not within your Fractions.h or Fractions.cpp, but rather with questionA.cpp note that the referencing file that cannot find the symbol in the error is:
c:\Users\VVV\documents\visual studio 2017\Projects\Homework2\Homework2\questionA.obj
Is #include "Fractions.h" in that file?

this error means that the compiler can't find this function.
write the specific place in the include path and see if it solve your problem.
if so, it means your header file is looking at different directory than the one you think.

Related

Error in VS2019 with Google Test when using outside definition

I just started a small console app in VS2019 and added a Google Test project. The program itself compiles and runs fine, however, if I move function code from the header to the cpp-file, I get the LNK2019 and LNK1120 errors:
Severity
Code
Description
Project
File
Line
Error
LNK2019
unresolved external symbol "public: void __cdecl MyClass::someFunction(void)" (?someFunction#MyClass##QEAAXXZ) referenced in function "private: virtual void __cdecl MyTests_basicTest_Test::TestBody(void)" (?TestBody#MyTests_basicTest_Test##EEAAXXZ)
Tests
Tests.obj
1
Error
LNK1120
1 unresolved externals
Tests
Tests.exe
1
MyClass.h:
#pragma once
class MyClass
{
public:
void someFunction();
};
MyClass.cpp:
#include "MyClass.h"
void MyClass::someFunction() {
// do something
}
Main.cpp:
#include "MyClass.h"
int main()
{
MyClass mc;
mc.someFunction();
}
Tests.cpp:
#include "pch.h"
#include "../TestApp/MyClass.h"
TEST(MyTests, basicTest) {
MyClass mc;
mc.someFunction();
EXPECT_EQ(1, 1);
}
I could just put all of the code in the header file of course, but I don't think that is what I'm supposed to do, so I would greatly appreciate some help on this.
Edit:
I feel like I'm one step closer (though it is only a shot in the dark): I added the directory that contains all the *.obj files under [Tests Project] -> "Properties" -> "Configuration Properties" -> "Linker" -> "Input" -> "Additional Dependencies": D:\Application\x64\Debug
However, now I get the following error:
LNK1104 cannot open file 'D:\Application\x64\Debug.obj'
And indeed, the directory contains all kinds of obj-files but not Debug.obj. :/
Edit 2:
Solved! I followed this link under "To link the tests to the object or library files". Important that you really just enter the base name of the file itself without the ".obj".

Cannot make function public. Getting: "LNK2005" error. How to debug linker errors

there is only one definition of MyFourierClass::forward_fft in my project. When I declare MyFourierClass::forward_fft as public, I get this error, otherwise there is no error.
Error Message:
1>my_fourier.obj : error LNK2005: "public: void __cdecl MyFourierClass::forward_fft(int)" (?forward_fft#MyFourierClass##QEAAXH#Z) already defined in main.obj
1>CGPProject\x64\Debug\CGPProject.exe : fatal error LNK1169: one or more multiply defined symbols found
my_fourier.cpp:
#ifndef MY_FOURIER
#define MY_FOURIER
class MyFourierClass {
double** dataset = 0;
//public: // <-- un-commenting this line causes the linker error.
void forward_fft(int);
};
void MyFourierClass::forward_fft(int bins) {
bins = bins + 1;
};
#endif
Main:
#ifndef MY_MAIN
#define MY_MAIN
#include "my_fourier.cpp"
int main() {
int i = 0;
}
#endif
is there a standard method for debugging linker errors? I thought there might be a definition in another file, so I've delete all other files in my project. There is now only main.cpp and my_fourier.cpp. I'm using Visual Studio 2019.
Thanks in advance.
As you included the module my_fourier.cpp in the module with main using the include directive
#include "my_fourier.cpp"
then this function
void MyFourierClass::forward_fft(int bins) {
bins = bins + 1;
};
is defined at least twice.
You should place the class definition in a header and this header include in the module
my_fourier.cpp where the member function is defined and in the module with main removing from the last module the directive
#include "my_fourier.cpp"

Error LNK2001: unresolved external symbol when trying to use source and header files from another directory than the default in Visual Studio 2015 [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 4 years ago.
I have the stdafx.h file and a source (test.cpp) file in the default directory (C:\Users\Roland\Desktop\projects\test\test) together with a header (header.h) and a source (source.cpp) file in an other directory (C:\Users\Roland\Desktop\projects\add) than the default one. The content of these files is the following:
stdafx.h: empty
test.cpp:
#include "stdafx.h"
#include <header.h>
int main()
{
int a = 2;
int b = 3;
swap(a, b);
return 0;
}
header.h:
#ifndef _HEADER_H_
#define _HEADER_H_
void swap(int &a, int &b);
#endif
source.cpp:
void swap(int &a, int &b)
{
int c;
c = a;
a = b;
b = c;
}
I added the following path to the "Additional Include Directory" record: ../../add. When trying to build the solution, I received an error:
error LNK2001: unresolved external symbol "void __cdecl swap(int &,int &)" (?swap##YAXAAH0#Z)
Could you please help me what would be the problem?
update:
I need a solution to making all the source files of the add directory available for my project.
If you have added source.cpp to your project, add source.h and put the swap function prototype in there. In your main function, include source.h and you should be fine.

Creating class object defined in another project

I have a Visual C++ solution with 2 projects: rectangle and project3.
In rectangle project I have rect.cpp and rect.h.
rect.h
#ifndef rect_h
#define rect_h
class Rect
{
public:
Rect();
int m_h;
int m_w;
};
#endif //rect_h
rect.cpp
#include "rect.h"
Rect::Rect()
{
m_h = 1;
m_w = 5;
}
whenever I try to create rect object from the rectangle project it succeeds.
But when I try to do the same from the project3 it produces a linker error.
LNK2019: unresolved external symbol "public: __thiscall
Rect::Rect(void)" (??0Rect##QAE#XZ) referenced in function _main
1>C:\Users\mbaro\documents\visual studio
2017\Projects\Project2\Debug\Project3.exe : fatal error LNK1120: 1
unresolved externals
main.cpp (in project 3)
#include "rect.h"
using namespace std;
int main()
{
Rect* a = new Rect();
return 0;
}
I kind of feel that class definition is picked up successfully, but the linker can not link the constructor code from rect.cpp.
What is the problem and how to solve it?
Thanks.
The error is normal: you told the compiler where it could find the .h files, but you did not tell the linker where it could find the .obj files.
It may depend on the exact VS version, but in Project/Properties, you should find Linker/Input and there Additional dependencies. If you only need one or two object files (xxx.obj) from the other project, add them here. That way, you avoid code duplication, which will be a nightmare for future maintenance...
If you have many common files, you should considere to put them in an auxilliary project that would build a (static)library in the same solution, and then link the library in both project (and of course give access to header files of the library project for the other projects using the library).
I have already started writing a long, long answer. Then i realized, what You may be missing is that despite Your class is named "Person" the header file You should have added is named "rect.h".
Also Your constructor cannot have a declaration of values in the header file (EDIT:not true, I was mistaken). In the header file, try using:
Person(int h, int w);
You declare what will be needed, not what You already have. If You want those to be specifically what You wrote the constructor should be:
Person();
in .h
and
Person::Person()
{
m_h = 1;
m_w = 5;
}
in .cpp.
If You need more detailed description of using include, I have already written a big part of it, so don't hesitate to ask.

C++ Cannot instantiate a class from another file in main.cpp

I cannot get this to work
Addr.h
#ifndef ADDR_H
#define ADDR_H
class Foo{
public:
Foo(); // function called the default constructor
Foo( int a, int b ); // function called the overloaded constructor
int Manipulate( int g, int h );
private:
int x;
int y;
};
#endif
Addr.cpp
#include "addr.h"
Foo::Foo(){
x = 5;
y = 10;
}
Foo::Foo( int a, int b ){
x = a;
y = b;
}
int Foo::Manipulate( int g, int h ){
return x = h + g*x;
}
main.cpp
#include "addr.cpp"
int main(){
Foo myTest = Foo( 20, 45 );
while(1){}
return 0;
}
What am i doing wrong? I get these linker errors:
error LNK2005: "public: int __thiscall Foo::Manipulate(int,int)" (?Manipulate#Foo##QAEHHH#Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(int,int)" (??0Foo##QAE#HH#Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(void)" (??0Foo##QAE#XZ) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK1169: one or more multiply defined symbols found c:\users\christian\documents\visual studio 2010\Projects\Console Test\Release\Console Test.exe
I would appreciate any kind of help!!!
#include "addr.cpp"
You're including the .cpp file to your main.cpp, which is what causing the problem. You should include the header file instead, as:
#include "addr.h" //add this instead, to your main.cpp
Because its the header file which is containing the definition of the class Foo.
In main.cpp, you should include the header file, rather than the cpp file.
Change
#include "addr.cpp"
To
#include "addr.h"
In main.cpp
#include "addr.cpp"
should be
#include "addr.h"
If you #include "addr.cpp", you can compile main.cpp (and even link it into an executable on it's own), but when you link main.obj and add.obj, you get a duplicate definition error, because class Foo is now fully defined in both object files.
You can either #include "addr.h" (as already stated), or make the build system not compile addr.cpp. The former is usually preferred, though the latter would also solve the trouble and is often used with automatic code generation tools' output.
There are two ways of getting the class data into main.cpp
1) include the header file "addr.h" in the main.cpp file, so now you have the class and member functions' definitions, but don't have the implementation part of them. So while compiling, the code should be :
g++ addr.cpp main.cpp.
2) include the cpp file "addr.cpp" in the main.cpp, so you automatically have the definitions also as "addr.h" is included in "addr.cpp". Now you must compile only the main.cpp file: g++ main.cpp.
First method is standard and is a good programming practice for maintaining modularity and encapsulation...