I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:
headertest.h:
#pragma once
class Cube
{
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
headertest.cpp:
#include "headertest.h"
double Cube::getVolume()
{
return length_ * length_ * length_;
}
double Cube::getSurfaceArea()
{
return 6 * length_ * length_;
}
void Cube::setLength(double length)
{
length_ = length;
}
and main.cpp:
#include "headertest.h"
#include <iostream>
int main()
{
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << volume;
}
However, when I try to build the files with Clang, I get the following error:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Does anyone have any idea of why this is happening? I've tried running g++ main.cpp headertest.cpp -o main before building, but the error isn't getting solved. Thank you!