I am learning about classes in c++, so I am writing the following simple code in separate files and while vscode is not showing any problems, the compiler is giving me an error when compiling.
main.cpp
#include <iostream>
#include "constants.h"
#include "cylinder.h"
int main ()
{
Cylinder cylinder1(10,10);
Cylinder cylinder2;
std::cout << "volume : " << cylinder1.volume() << std::endl;
return 0;
}
cylinder.h
#ifndef CYLINDER_H
#define CYLINDER_H
#include "constants.h"
class Cylinder {
public:
Cylinder () = default;
Cylinder (double rad_param, double height_param);
double volume () { return PI * base_radius * base_radius * height; }
double get_base_radius () { return base_radius; }
double get_height () { return height; }
void set_base_radius (double rad_param) {base_radius = rad_param; }
void set_height (double height_param) {height = height_param; }
private:
double base_radius {1};
double height {1};
};
#endif
cylinder.cpp
#include "cylinder.h"
Cylinder::Cylinder (double rad_param, double height_param)
{
base_radius = rad_param;
height = height_param;
}
constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
const double PI {3.1415926535897932384626433832795};
#endif
When I try to compile, I get this error:
Starting build... C:\mingw64\bin\g++.exe -fdiagnostics-color=always -g -std=c++23 "C:\PCHDD\Programming PCHDD\Learning\Template\main.cpp" -o "C:\PCHDD\Programming PCHDD\Learning\Template\main.exe" C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\90539\AppData\Local\Temp\cc6YTSXM.o: in function
main': C:/PCHDD/Programming PCHDD/Learning/Template/main.cpp:9:(.text+0x30): undefined reference toCylinder::Cylinder(double, double)' collect2.exe: error: ld returned 1 exit statusBuild finished with error(s).
The terminal process terminated with exit code: -1. Terminal will be reused by tasks, press any key to close it.
I did some search and found that it may be a linker error caused by not implementing a function, but all functions are implemented in the main.cpp ! I don't know what I'm doing wrong.
Thanks in advance for your help.