我有以下c ++代码,可以在使用g ++的终端中很好地进行编译。
g++ pointTypeMain.cpp pointTypeImp.cpp -o pointType
但是,当我尝试在IDE中运行它时却失败了。这是链接问题。但我不确定该如何解决。
// pointType.h
#ifndef H_PointType
#define H_PointType
class pointType
{
public:
void setPoint(double x, double y);
void print() const;
double getX() const;
double getY() const;
pointType(double x = 0.0, double y = 0.0);
protected:
double xCoordinate;
double yCoordinate;
};
#endif
//Implementation File pointTypeImp.cpp
#include <iostream>
#include "pointType.h"
using namespace std;
void pointType::setPoint(double x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
void pointType::print() const
{
cout << "(" << xCoordinate << ", "
<< yCoordinate << ")" << endl;
}
double pointType::getX() const
{
return xCoordinate;
}
double pointType::getY() const
{
return yCoordinate;
}
pointType::pointType(double x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
// Main implementation
#include <iostream>
#include <iomanip>
#include "pointType.h"
using namespace std;
int main()
{
pointType point1(3.5, 2.5);
pointType point2;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Point 1: ";
point1.print();
cout << endl;
point2.setPoint(-2.5, 7);
cout << "Point 2: ";
point2.print();
cout << endl;
return 0;
}
O / p错误:
====================[ Build | pointType | Debug ]===============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/vpangul/CLionProjects/pointType/cmake-build-debug --target pointType -- -j 4
[ 50%] Linking CXX executable pointType
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)
make[3]: *** [pointType] Error 1
make[2]: *** [CMakeFiles/pointType.dir/all] Error 2
make[1]: *** [CMakeFiles/pointType.dir/rule] Error 2
make: *** [pointType] Error 2
当我尝试在IDE的(eclipse,CLion)中构建它时,出现以下错误。有人可以帮忙吗?附加项目属性的屏幕截图。Project toolchain properties