简体   繁体   English

C ++中编译类时出现错误

[英]errors when compiling class in c++

I tried to create a class for 3-D vector in c++, but there are some errors. 我试图在c ++中为3-D向量创建一个类,但是存在一些错误。 I have learnt a little bit of oop in python before, but I'm still pretty new to oop and c++. 我以前在python中学过一点oop,但是对oop和c ++还是很新的。 I created the header file threevector.h, the file for the class threevector.cpp and the main program file main.cpp. 我创建了头文件threevector.h,类Threevector.cpp的文件和主程序文件main.cpp。 I just want to know what I have done wrong. 我只想知道我做错了什么。

// 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;
}

The following are the error messages I get: 以下是我收到的错误消息:

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

You defined your threevector 's methods wrong. 您将threevector的方法定义错误。 It should be: 它应该是:

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;
}

All threevector 's methods are in threevector 's scope. 所有threevector的方法都在threevector的范围内。

Don't forget to compile it as same as main.cpp : 不要忘记像main.cpp一样编译它:

g++ threevector.cpp main.cpp

This will be linked properly as threevector is in same object file. 这将被正确链接,因为threevector在同一目标文件中。

You have not defined your member function, you only declared them. 您尚未定义成员函数,只声明了它们。

If you look at your class definition again: 如果再次查看您的类定义:

 class threevector { private: double xcoord, ycoord, zcoord; public: threevector(); threevector(double x, double y, double z, char type); void print (); }; 

It contains declarations for three member functions: 它包含三个成员函数的声明:

  • threevector::threevector()
  • threevector::threevector(double, double, double, char)
  • threevector::print()

The threevector:: part is important - since these are member functions, the class name is part of their own name (or so to speak). threevector::部分很重要-由于这些是成员函数,因此类名是其自身名称的一部分(或可以这么说)。

The functions you defined, however, miss the threevector:: part. 但是,您定义的函数缺少threevector::部分。 For example, you define a function called print() , but the name is just a coincidence. 例如,您定义了一个名为print()的函数,但名称只是一个巧合。

threevector::print() and print() are different names and thus different functions. threevector::print()print()是不同的名称,因此具有不同的功能。

Same with the two constructors. 与两个构造函数相同。

The solution is therefore to use the correct, full names when you define the functions: 因此,解决方案是在定义函数时使用正确的全名:

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;
}

While we're at it, constructors should use initialisation lists, not assignment, at least if that's easily possible: 在此过程中,构造函数应该使用初始化列表,而不是赋值,至少在可能的情况下:

threevector::threevector() :
    xcoord(0.0),
    ycoord(0.0),
    zcoord(0.0)
{
}

PS: Do not confuse this with namespaces. PS:请勿将此与命名空间混淆。 All of this has nothing to do with namespaces. 所有这些都与名称空间无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM