C++;visualstudio代码中的链接和编译问题 现在我还在学校里学习C++,现在我对学习C++仍然很有新意。
我在编译我的两个.cpp文件时遇到了一些问题,因为我得到了“未定义的引用”。为了解决其中的一些问题,我在互联网上做了大量的挖掘工作,比如对tasks.json进行了一些小的编辑,甚至尝试在终端窗口中通过命令行进行编译,但似乎没有任何效果。对所有文件也都在同一个项目文件夹中。这让我头疼得厉害 这是我的密码: Asi2.cppC++;visualstudio代码中的链接和编译问题 现在我还在学校里学习C++,现在我对学习C++仍然很有新意。,c++,linker,compiler-construction,undefined,C++,Linker,Compiler Construction,Undefined,我在编译我的两个.cpp文件时遇到了一些问题,因为我得到了“未定义的引用”。为了解决其中的一些问题,我在互联网上做了大量的挖掘工作,比如对tasks.json进行了一些小的编辑,甚至尝试在终端窗口中通过命令行进行编译,但似乎没有任何效果。对所有文件也都在同一个项目文件夹中。这让我头疼得厉害 这是我的密码: Asi2.cpp #include <iostream> #include "Point.h" using namespace std;
#include <iostream>
#include "Point.h"
using namespace std;
int main()
{
Point point1(3.0, 6.0);
Point point2(-4.5, 2.2);
cout << "Distance between points: " << point1.calculateDistance(point2) << "\n";
return 0;
}
尝试编译和运行驱动程序文件(Asi2.cpp)时获得的输出:
PS C:\Users\Luke\Documents\VSC\Asi 2>cd“C:\Users\Luke\Documents\VSC\Asi 2”;if($?){g++Asi2.cpp-o Asi2};如果($?){.\Asi2}
C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x30):对Point::Point(float,float)”的未定义引用C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x50):对Point::Point(float,float)的未定义引用
C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x79):未定义对Point::Point(Point&)的引用C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x8c):未定义对Point::calculatestance(Point)'
C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xb6):对点::~Point()的未定义引用C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xc5):对点:~Point()的未定义引用
C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xcf):未定义对点::~Point()的引用C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xdf):未定义对点:~Point()的引用
C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xed):对点::~Point()的未定义引用C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xfb):接下来是对点:~Point()的未定义引用
collect2.exe:错误:ld在命令行“g++…”中返回了1个退出状态
,没有提到Point.cpp,因此它不会被编译。这就是为什么您有这些未定义的引用。您必须添加它,看到Visual Studio代码的正确配置会让很多人感到困惑。你有更简单的选择吗?你真的不想在学习一种更复杂的开发工具的同时学习其中一种最复杂编程语言的技巧,因为在你至少熟悉一种之前,你将很难确定哪种语言在欺骗你。顺便说一句get(void)
是C风格的。C++风格不包括#include <iostream>
#include <math.h>
#include "Point.h"
using namespace std;
Point :: Point()
{
x = 0.0;
y = 0.0;
}
Point :: Point(float x1, float y1)
{
x = x1;
y = y1;
}
Point :: Point(Point &point1)
{
x = point1.getX();
y = point1.getY();
}
Point :: ~Point()
{
}
void Point :: move(float x1, float y1)
{
x = x1;
y = y1;
}
float Point :: calculateDistance(Point point1)
{
double distance;
double cal = ((point1.getX() - x) * (point1.getX() - x)) + ((point1.getY() - y) * (point1.getY() - y));
distance = sqrt(cal);
return distance;
}
void Point :: translate (float dx, float dy)
{
x += dx;
y += dy;
}
void Point :: position (Point point1)
{
if(x == point1.getX() && y == point1.getY())
{
cout << "These points are in the SAME spot!\n";
}
if(x == point1.getX() && y > point1.getY())
{
cout << "The selected point is BELOW the current point.\n";
}
if(x == point1.getX() && y < point1.getY())
{
cout << "The selected point is ABOVE the current point.\n";
}
if(x > point1.getX() && y == point1.getY())
{
cout << "The selected point is to the LEFT of the current point.\n";
}
if(x < point1.getX() && y == point1.getY())
{
cout << "The selected point is to the RIGHT of the current point\n.";
}
if(x < point1.getX() && y < point1.getY())
{
cout << "The selected point is to the RIGHT and ABOVE the current point\n.";
}
if(x > point1.getX() && y > point1.getY())
{
cout << "The selected point is to the LEFT and BELOW the current point\n.";
}
if(x < point1.getX() && y > point1.getY())
{
cout << "The selected point is to the RIGHT and BELOW the current point\n.";
}
if(x > point1.getX() && y < point1.getY())
{
cout << "The selected point is to the LEFT and ABOVE the current point\n.";
}
}
float Point :: getX(void)
{
return x;
}
float Point :: getY(void)
{
return y;
}
void Point :: setX(float x1)
{
x = x1;
}
void Point :: setY(float y1)
{
x = y1;
}
float Point :: print(void)
{
return x, y;
}
#ifndef POINT_H
#define POINT_H
class Point
{
private:
float x;
float y;
public:
Point();
Point(float x1, float y1);
Point(Point &point1);
~Point();
void move(float x1, float y1);
float calculateDistance(Point point1);
void translate(float dx, float dy);
void position(Point point1);
float getX(void);
float getY(void);
void setX(float x1);
void setY(float y1);
float print(void);
};
#endif