Linker Error but don't know where the problem is

Hello everyone,

I have a small C++ project in Visual Studio 2019, but I am getting a linker error in connection with my Mesh.cpp class. I don't really know where it is coming from since all the .obj files exist and can't see what I did wrong in my code. Maybe some of you can point out where I messed up, I'd appreciate every help I can get.

This is the error I am getting:
 
Error	LNK2019	unresolved external symbol "public: __cdecl Mesh::Mesh(float)" (??0Mesh@@QEAA@M@Z) referenced in function "void __cdecl test_function(void)" (?test_function@@YAXXZ)


This is the Test.cpp file where I am getting the linker error when trying to create the Mesh object:

1
2
3
4
5
#include "Mesh.h"

void test_function() {
    Mesh m(0.5f);
}


The Mesh.cpp file:

1
2
3
4
5
6
7
class Mesh {
	float test_var;

public:
	Mesh(float f) : test_var(f){};

};


The Mesh.h file:
1
2
3
4
5
6
7
8
9
10
11
#ifndef MESH_H
#define MESH_H

class Mesh {
	float test_var;

public:
	Mesh(float);
};

#endif 


One thing I tried is removing the float argument in the Mesh constructor, so basically changing the constructor to Mesh(); instead of Mesh(float); and passing nothing to it, which worked. But as soon as I put in a parameter that is to be expected, the linker error happens.
You are defining a non-standard, non-member function in Mesh.cpp, you should be defining the constructor you declared in Mesh.h. You don't need to redeclare your class.

1
2
3
#include "Mesh.h"

Mesh::Mesh(float f) : test_var(f) { };


With as simple a constructor as you've created you could just inline the definition in the header and not need a separate .cpp source file for the class:

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef MESH_H
#define MESH_H

class Mesh
{
	float test_var;

public:
	Mesh(float f) : test_var(f) { };
};

#endif  
Just for "gits and shiggles" I converted the above to use C++20 modules, something VS2019 (and VS2022) can use.*

Mesh.cppm (module interface)
1
2
3
4
5
6
7
8
9
export module Mesh;

export class Mesh
{
   float test_var { };

public:
   Mesh(float f);
};


Mesh.cpp (module internal partition, has to be manually declared as a partition in the project properties)
1
2
3
4
5
6
7
8
9
export module Mesh;

export class Mesh
{
   float test_var { };

public:
   Mesh(float f);
};


For testing completeness I created a fully functional test source file that included a rump main (Test.cpp):
1
2
3
4
5
6
7
8
9
import Mesh;

void test_function()
{
   Mesh m(0.5f);
}

int main()
{ }


This compiles cleanly in both VS2019 & 2022.

*If you use C++ headers as import modules you should set the project's C++ language standard to compile against /std:c++latest (Preview) instead of /std:c++20.

It certainly won't hurt if you set the project's C++ language standard to Preview status anyway. VS2019/2022 default to C++14.
Your answers helped me getting my code to actually work now, thank you so much for your tips and time!
Topic archived. No new replies allowed.