使用头文件和源文件时出错

我创建了一个头文件,调用它One.h来包含我的 3d 矢量类,然后我使用一个被调用的源文件main.cxx将该类输入到一个 int 函数中。但是,我不断收到这些错误,并且我已经尝试了几天来修复它,但我不能。我是 C++ 的新手。

/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:165:53:
error: no type named ‘iterator_category’ in ‘class Vector3d’
       typedef typename _Iterator::iterator_category iterator_category;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:166:53: error:
no type named ‘value_type’ in ‘class Vector3d’
       typedef typename _Iterator::value_type        value_type;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:167:53: error:
no type named ‘difference_type’ in ‘class Vector3d’
       typedef typename _Iterator::difference_type   difference_type;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:168:53: error:
no type named ‘pointer’ in ‘class Vector3d’
       typedef typename _Iterator::pointer           pointer;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:169:53: error:
no type named ‘reference’ in ‘class Vector3d’
       typedef typename _Iterator::reference         reference;
                                                     ^ 

一小时

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

class Vector3d {
// Class of a Euclidean 3d vector
private:
    float x;
    float y;
    float z;

    // The x y and z components that are our private data members

public:
    Vector3d(float xaxis, float yaxis, float zaxis)
    {
        x = xaxis;
        y = yaxis;
        z = zaxis;
    }

    // Creating a constructor to help access the members of the class

    float getx() { return x; }
    float gety() { return y; }
    float getz() { return z; }

    // Used to actually get the values of our members so functions to print out values

    float normVector()
    {
        float result = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
        return result;
        // The norm of the vector an function with in the class
    }
};

float distance(Vector3d v1, Vector3d v2)
// We have a function outside the class, the arguments are objects of the same class but two different objects
// so like two different vectors
{
    return sqrt(
      pow(v1.getx() - v2.getx(), 2)
    + pow(v1.gety() - v2.gety(), 2)
    + pow(v1.getz() - v2.getz(), 2)
       );

    // In the function we can use v1.getx and v2.getx to access and put the values of the private members in the class

    // This is the distance formula between to vectors

主文件

#include "One.h"

int main()
{
    Vector3d v1(2, 2, 2);

    v1.getx();
    v1.gety();
    v1.getz();

    // Our vector one with defiining values

    v1.normVector();

    cout << "  Components and the norm of my first vector     " << endl;
    cout << " X1 axis t:t "   << v1.getx() << endl;
    cout << " Y1 axis  t:t "  << v1.gety() << endl;
    cout << " Z1 axis   t:t "   << v1.getz() << endl;

    cout << " The norm of our first vector  t:t " << v1.normVector() << endl;

    Vector3d v2(3, 3, 3);

    v2.getx();
    v2.gety();
    v2.getz();

    cout << "  Components and the norm of my second vector     " << endl;

    cout << " X2  axis t:t " << v2.getx() << endl;
    cout << " Y2  axis  t:t " << v2.gety() << endl;
    cout << " Z2  axis t:t " << v2.getz() << endl;

    cout << " The norm of our second vector " << v2.normVector() << endl;

    cout << "The distance between vector one and two t:t " << distance(v1, v2) << " units." << endl;

    // Here we can use the cout operator to compile the distance formula we created earlier and make it visible on the screen

    return 0;
}

回答

std::distance和你的float distance(Vector3d v1, Vector3d v2)功能之间有冲突。

在必要时移除using namespace std;并获得资格std::将解决该问题。

这是为什么建议很少使用(如果有的话)的一个例子using namespace std;


以上是使用头文件和源文件时出错的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>