为什么崇高在引用头文件后找不到自定义类?

时间:2019-05-29 07:18:02

标签: c++

我正在学习 C ++ Primer Plus 一书,书中有一些我想实现的代码,但是使用Sublime Text 3进行编码时会遇到一些问题。 我已经在主文件中引用了头文件,但找不到它。我的代码有问题吗?还是Sublime有点问题? 顺便说一下,头文件和主文件都在同一个文件中。

在CPrimerPlus.h文件中:

#ifndef CPRIMERPLUS_H_
#define CPRIMERPLUS_H_

#include <iostream>

namespace VECTOR {
    class Vector {
        public:
            enum Mode {RECT, POL};

        private:
            double x;
            double y;
            double mag;
            double ang;
            Mode mode;

            void set_mag();
            void set_ang();
            void set_x();
            void set_y();

        public:
            Vector();
            Vector(double n1, double n2, Mode form = RECT);
            void reset(double n1, double n2, Mode form = RECT);
            ~Vector();
            double xval() const {return x;}
            double yval() const {return y;}
            double magval() const {return mag;}
            double angval() const {return ang;}
            void polar_mode();
            void rect_mode();
            Vector operator+(const Vector & b) const;
            Vector operator-(const Vector & b) const;
            Vector operator-() const;
            Vector operator*(double) const;
            friend Vector operator*(double n, const Vector & a);
            friend std::ostream & operator<<(std::ostream & os, const Vector & v);
    };
}

#endif

在CPrimerPlus.cpp文件中:

#include "CPrimerPlus.h"
#include <cmath>

using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR {
const double Rad_to_deg = 45.0;

void Vector::set_mag() {
    mag = sqrt(x * x + y * y);
}

void Vector::set_ang() {
    if(x == 0.0 && y == 0.0)
        ang = 0.0;
    else
        ang = atan2(y, x);
}

void Vector::set_x() {
    x = mag * cos(ang);
}

void Vector::set_y() {
    y = mag * sin(ang);
}

Vector::Vector() {
    x = y = mag = ang = 0.0;
    mode = RECT;
}

Vector::Vector(double n1, double n2, Mode form) {
    mode = form;
    if(form == RECT) {
        x = n1;
        y = n2;
        set_mag();
        set_ang();
    }
    else if(form == POL) {
        mag = n1;
        ang = n2 / Rad_to_deg;
        set_x();
        set_y();
    }
    else {
        cout << "Icorrect 3rd argument to Vector() --";
        cout << "Vector set to 0\n";
        x = y = mag = ang = 0.0;
        mode = RECT;
    }
}

void Vector::reset(double n1, double n2, Mode form) {
    mode = form;
    if(form == RECT) {
        x = n1;
        y = n2;
        set_mag();
        set_ang();
    }
    else if(form == POL) {
        mag = n1;
        ang = n2 / Rad_to_deg;
        set_x();
        set_y();
    }
    else {
        cout << "Icorrect 3rd argument to Vector() --";
        cout << "Vector set to 0\n";
        x = y = mag = ang = 0.0;
        mode = RECT;            
    }
}

Vector::~Vector() {

}

void Vector::polar_mode() {
    mode = POL;
}

void Vector::rect_mode() {
    mode = RECT;
}

Vector Vector::operator+(const Vector & b) const {
    return Vector(x + b.x, y + b.y);
}

Vector Vector::operator-(const Vector & b) const {
    return Vector(x - b.x, y - b.y);
}

Vector Vector::operator-() const {
    return Vector(-x, -y);
}

Vector Vector::operator*(double n) const {
    return Vector(n * x, n * y);
}

Vector operator*(double n, const Vector & a) {
    return a * n;
}

std::ostream & operator<<(std::ostream & os, const Vector & v) {
    if(v.mode == Vector::POL)
        os << "(x, y) = (" << v.x << ", " << v.y << ")";
    else if(v.mode == Vector::POL) {
        os << "(m, a) = (" << v.mag << ", " << v.ang * Rad_to_deg << ")";
    }
    else
        os << "Vector object mode is invalid";
    return os;
}
} 

在主文件中:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "CPrimerPlus.h"

int main() {
    using namespace std;
    using VECTOR::Vector;
    srand(time(0));
    double direction;
    Vector step;
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    double target;
    double dstep;
    cout << "Enter target distance (q to quit): ";
    while(cin >> target) {
        cout << "Enter step length: ";
        if(!(cin >> dstep))
            break;

        while(result.magval() < target) {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
        }
        cout << "After " << steps << " steps, the subject has the following location:\n";
        cout << result << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        cout << "Average outward distance per step = " << result.magval() / steps << endl;
        steps = 0;
        result.reset(0.0, 0.0);
        cout << "Enter target distane (q to quit): ";
    }
    cout << "Bye!\n";
    cin.clear();
    while(cin.get() != '\n')
        continue;
    return 0;
}

编译器找不到头文件。

  

d:/ tools / mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:   C:\ Users \ HEY \ AppData \ Local \ Temp \ ccopioMY.o:main.cpp :(。text + 0x5c):   未定义对`VECTOR :: Vector :: Vector()'的引用

     

collect2.exe:错误:ld返回1个退出状态

0 个答案:

没有答案