【问题标题】:Strange error in c++ class constructionc++类构造中的奇怪错误
【发布时间】:2017-05-17 09:22:52
【问题描述】:

我最近开始学习 c++,现在我正在尝试制作一个简单的矢量类作为练习。但不知何故,我的代码似乎不起作用。

#include <iostream>
#include <cmath>
class Vec2
{
public:
    float x1;
    float x2;
    Vec2(float a,float b):x1(a),x2(b){}
    float norm()
    {
        return sqrt(x1*x1+x2*x2);
    }
    Vec2 operator+(const Vec2 &v)
    {
        Vec2 newv;
        newv.x1=this->x1+v.x1;
        newv.x2=this->x2+v.x2;
        return newv;
    }
};
int main()
{
    Vec2 v1(3,4);
    Vec2 v2(4,5);
    Vec2 v3=v1+v2;
    std::cout << v1.x1 << std::endl;
    std::cout << v1.norm() << std::endl;
    std::cout << v3.x1 << std::endl;
    return 0;
}

我使用eclipse作为编辑器,编译时出现这个错误:

11:13:04 **** Incremental Build of configuration Debug for project Vec2 ****
make all 
Building file: ../Vec2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Vec2.d" -MT"Vec2.o" -o "Vec2.o" "../Vec2.cpp"
../Vec2.cpp: In member function ‘Vec2 Vec2::operator+(const Vec2&)’:
../Vec2.cpp:15:11: error: no matching function for call to ‘Vec2::Vec2()’
      Vec2 newv;
           ^~~~
../Vec2.cpp:8:2: note: candidate: Vec2::Vec2(float, float)
  Vec2(float a,float b):x1(a),x2(b){}
  ^~~~
../Vec2.cpp:8:2: note:   candidate expects 2 arguments, 0 provided
../Vec2.cpp:3:7: note: candidate: constexpr Vec2::Vec2(const Vec2&)
 class Vec2
       ^~~~
../Vec2.cpp:3:7: note:   candidate expects 1 argument, 0 provided
../Vec2.cpp:3:7: note: candidate: constexpr Vec2::Vec2(Vec2&&)
../Vec2.cpp:3:7: note:   candidate expects 1 argument, 0 provided
make: *** [subdir.mk:20: Vec2.o] Error 1

11:13:05 Build Finished (took 422ms)

我怀疑运算符重载是这里的罪魁祸首,但我似乎无法让它运行。 任何想法将不胜感激!

【问题讨论】:

  • 为您的类提供一个默认构造函数,一切顺利
  • 运算符重载与您的问题无关。您需要阅读有关构造函数的更多信息(例如 C++ rule of five

标签: c++ class vector overloading


【解决方案1】:

您的类缺少默认构造函数。只需添加一个即可完成

class Vec2
{
public:
    float x1;
    float x2;
    Vec2() {}  // default constructor
};

【讨论】:

    【解决方案2】:

    问题不在于运算符重载。该错误消息将帮助您了解这是一个构造函数问题。

    来自this 参考,如果您有用户定义的构造函数,则该类不存在默认构造函数。因此,您的程序需要构造函数中的参数,而您没有提供。

    解决方法是定义一个默认构造函数,以及您已经定义的构造函数。这样你就可以同时使用了。

    Vec2() {}
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      你可以创建默认构造函数

      Vec2() {}
      

      或者您可以将第 15-17 行更改为

      Vec2 newv(this->x1+v.x1, newv.x2=this->x2+v.x2);
      

      【讨论】:

        【解决方案4】:

        我们不需要这个类的默认构造函数。 这意味着用户不能在没有 x,y 参数的情况下创建对象。 如果您打算以这种方式吸引用户,我认为这可能是一个很好的理由。

        #include <iostream>
        #include <cmath>
        class Vec2
        {
        public:
            float x1;
            float x2;
            Vec2(float a,float b):x1(a),x2(b){}
            float norm()
            {
                return sqrt(x1*x1+x2*x2);
            }
            Vec2 operator+(const Vec2 &v)
            {
                float x1=this->x1+v.x1;
                float x2=this->x2+v.x2;
                Vec2 newv(x1,x2);
                return newv;
            }
        };
        int main()
        {
            Vec2 v1(3,4);
            Vec2 v2(4,5);
            Vec2 v3=v1+v2;
            std::cout << v1.x1 << std::endl;
            std::cout << v1.norm() << std::endl;
            std::cout << v3.x1 << std::endl;
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-07
          • 1970-01-01
          • 1970-01-01
          • 2021-07-16
          • 2013-09-05
          • 1970-01-01
          • 2018-08-04
          相关资源
          最近更新 更多