【问题标题】:declaration of 'OPairType::OPairType(int, int)' outside of class is not definition'OPairType::OPairType(int, int)' 在类之外的声明不是定义
【发布时间】:2023-03-03 03:22:01
【问题描述】:

我得到的错误是:

在类之外声明'OPairType::OPairType(int, int)' 不是定义-fpermissive

这是我的完整代码:

头文件:

class OPairType{
private:
 int x;
 int y;
public:
 OPairType (int=0, int=0);
 int getX() const;
 int getY() const;
 void setX(int);
 void setY(int);
 void setValues(int, int);
 friend OPairType operator + (OPairType, OPairType);
 friend OPairType operator - (OPairType, OPairType);
 friend bool operator == (OPairType, OPairType);
 friend bool operator != (OPairType, OPairType);
 friend std::ostream& operator << (std::ostream&, OPairType);

这是.cpp代码:

#include "OPairType.h"
#include <iostream>

OPairType::OPairType (int x, int y); //error occurs here

int OPairType::getX() const {
 return x;
}

int OPairType::getY() const {
 return y;
}

void OPairType::setX(int new_x) {
 x = new_x;
}

void OPairType::setY(int new_y) {
 y = new_y;
}

void OPairType::setValues (int new_x, int new_y){
 x = new_x;
 y = new_y;
}

OPairType operator + (OPairType lh, OPairType rh){
OPairType answer;

 answer.x = lh.x + rh.x;
 answer.y = lh.y + rh.y;

 return answer;
}

OPairType operator - (OPairType lh, OPairType rh){
OPairType answer;

 answer.x = lh.x - rh.x;
 answer.y = lh.y - rh.y;

 return answer;
}

bool operator == (OPairType lh, OPairType rh){
 return lh.x == rh.x && lh.y == rh.y;
}

bool operator != (OPairType lh, OPairType rh){
 return !(lh.x == rh.x && lh.y == rh.y);
}

std::ostream& operator << (std::ostream& out, OPairType c){
 out << "(" << c.x << ", " << c.y << ")";
 return out;
}

【问题讨论】:

    标签: c++ class operators


    【解决方案1】:

    你的意思可能是:

    OPairType::OPairType (int x, int y){ }
    

    你不能只在类之外声明一个成员函数。您需要定义主体。

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多