在 C++ 中编译 class 时出错
errors when compiling class in c++
我试图在 C++ 中为 3-D 向量创建一个 class,但是有一些错误。我以前在 python 中学过一点 oop,但我对 oop 和 c++ 还是很陌生。我创建了头文件 threevector.h、class threevector.cpp 的文件和主程序文件 main.cpp。我只想知道我做错了什么。
// threevector.h
#ifndef THREEVECTOR_H
#define THREEVECTOR_H
#include <iostream>
class threevector {
private:
double xcoord, ycoord, zcoord;
public:
threevector();
threevector(double x, double y, double z, char type);
void print ();
};
#endif // THREEVECTOR_H
//threevector.cpp
#include "threevector.h"
#include <cmath>
threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
// main.cpp
#include "threevector.h"
#include <cmath>
#include <iostream>
using namespace std;
int main() {
threevector v0;
v0.print();
threevector v1(-1,2,4.384,'c');
cout << "v1 = ";
v1.print();
return 0;
}
以下是我收到的错误信息:
main.cpp(.text+0x15): undefined reference to 'threevector::threevector()'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
main.cpp(.text+0x15): undefined reference to 'threevector::threevector::threevector(double, double, double, char)'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
[Error] ld returned 1 exit status
您对 threevector
方法的定义有误。应该是:
threevector::threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector::threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void threevector::print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
所有threevector
的方法都在threevector
的范围内。
不要忘记像main.cpp
一样编译它:
g++ threevector.cpp main.cpp
这将被正确链接,因为 threevector
在同一个目标文件中。
你还没有定义你的成员函数,你只是声明了它们。
如果您再次查看 class 定义:
class threevector {
private:
double xcoord, ycoord, zcoord;
public:
threevector();
threevector(double x, double y, double z, char type);
void print ();
};
它包含三个成员函数的声明:
threevector::threevector()
threevector::threevector(double, double, double, char)
threevector::print()
threevector::
部分很重要 - 因为这些是成员函数,class 名称是它们自己名称的一部分(或者可以这么说)。
但是,您定义的函数缺少 threevector::
部分。比如你定义了一个函数叫print()
,但是这个名字只是巧合。
threevector::print()
和 print()
是不同的名称,因此具有不同的功能。
与两个构造函数相同。
因此,解决方案是在定义函数时使用正确的全名:
threevector::threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector::threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void threevector::print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
尽管如此,构造函数应该使用初始化列表,而不是赋值,至少如果这很容易的话:
threevector::threevector() :
xcoord(0.0),
ycoord(0.0),
zcoord(0.0)
{
}
P.S.: 不要将它与命名空间混淆。所有这些都与名称空间无关。
我试图在 C++ 中为 3-D 向量创建一个 class,但是有一些错误。我以前在 python 中学过一点 oop,但我对 oop 和 c++ 还是很陌生。我创建了头文件 threevector.h、class threevector.cpp 的文件和主程序文件 main.cpp。我只想知道我做错了什么。
// threevector.h
#ifndef THREEVECTOR_H
#define THREEVECTOR_H
#include <iostream>
class threevector {
private:
double xcoord, ycoord, zcoord;
public:
threevector();
threevector(double x, double y, double z, char type);
void print ();
};
#endif // THREEVECTOR_H
//threevector.cpp
#include "threevector.h"
#include <cmath>
threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
// main.cpp
#include "threevector.h"
#include <cmath>
#include <iostream>
using namespace std;
int main() {
threevector v0;
v0.print();
threevector v1(-1,2,4.384,'c');
cout << "v1 = ";
v1.print();
return 0;
}
以下是我收到的错误信息:
main.cpp(.text+0x15): undefined reference to 'threevector::threevector()'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
main.cpp(.text+0x15): undefined reference to 'threevector::threevector::threevector(double, double, double, char)'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
[Error] ld returned 1 exit status
您对 threevector
方法的定义有误。应该是:
threevector::threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector::threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void threevector::print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
所有threevector
的方法都在threevector
的范围内。
不要忘记像main.cpp
一样编译它:
g++ threevector.cpp main.cpp
这将被正确链接,因为 threevector
在同一个目标文件中。
你还没有定义你的成员函数,你只是声明了它们。
如果您再次查看 class 定义:
class threevector { private: double xcoord, ycoord, zcoord; public: threevector(); threevector(double x, double y, double z, char type); void print (); };
它包含三个成员函数的声明:
threevector::threevector()
threevector::threevector(double, double, double, char)
threevector::print()
threevector::
部分很重要 - 因为这些是成员函数,class 名称是它们自己名称的一部分(或者可以这么说)。
但是,您定义的函数缺少 threevector::
部分。比如你定义了一个函数叫print()
,但是这个名字只是巧合。
threevector::print()
和 print()
是不同的名称,因此具有不同的功能。
与两个构造函数相同。
因此,解决方案是在定义函数时使用正确的全名:
threevector::threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector::threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void threevector::print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
尽管如此,构造函数应该使用初始化列表,而不是赋值,至少如果这很容易的话:
threevector::threevector() :
xcoord(0.0),
ycoord(0.0),
zcoord(0.0)
{
}
P.S.: 不要将它与命名空间混淆。所有这些都与名称空间无关。