我已经为用户定义的 StringSet 类重载了 + 运算符,它将两个 StringSet 联合起来。
StringSet& StringSet::operator+(StringSet& set2) {
StringSet* temp = new StringSet;
for (auto i = 0; i < this->getSize(); i++) {
temp->addSet(this->data[i]);
}
for (auto j = 0; j < set2.getSize(); j++) {
temp->addSet(set2.data[j]);
}
return *temp;
}
Run Code Online (Sandbox Code Playgroud)
如果我不动态分配 temp 则无法将其用作
cout << "union is: " << s1+s2 << endl; // s1 and s2 are StringSet objs created using constructor
Run Code Online (Sandbox Code Playgroud)
当前代码对此工作正常,但是,我想知道是否可以在不动态分配 temp 或分配然后以某种方式删除它的情况下执行此操作。
我正在尝试创建一个使用有理数字并对它们执行运算符重载的类.我在程序的一部分,即输入流上遇到问题.
例如,我应该以"12/8"格式输入,它应该将12存储到变量a中,然后将8存储到变量b中.
这是我的代码:
istream& operator>>( istream& In, Rational& Item )
{
char division_sign;
int a,b;
In >> a >> division_sign;
if (division_sign != '/' || !In.good())
{
In.setstate( ios::failbit );
}
else
{
In >> b;
if (b != 0 || !In.good())
{
return Item.numerator_ = a, Item.denominator_ = b;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
In function 'std::istream& operator>>(std::istream&, Rational&)':
131: error: invalid initialization of reference of type 'std::istream&' from expression of type 'int'
Run Code Online (Sandbox Code Playgroud)
Line 131是return声明
我写了一个类,我有一个添加矩阵的问题.我知道我必须重载operator +,但我不知道究竟是怎么回事.有任何想法吗 ?
class CMatrix
{
private:
int Rows;
int Columns;
float* pData;
public:
CMatrix(void);
CMatrix(int rows, int columns);
void setElement(int row, int column, float element);
float getElement(int row, int column);
...};
istream& operator>>(istream& in, CMatrix& matrix)
{
in >> matrix.Rows;
in >> matrix.Columns;
for(int i = 0; i < matrix.Rows; i++)
for(int j = 0; j < matrix.Columns; j++)
in >> *(matrix.pData + i * matrix.Columns + j);
return in;
}
CMatrix::CMatrix(int rows, int columns)
{
Rows = rows; …Run Code Online (Sandbox Code Playgroud) 我不经常涉及C++,但我正在学习数据结构,本书使用c ++作为语言.我现在正在设置课程.
我的问题是:
Visual Studio 2012正在咆哮着未知的变量.我在我的标题中声明了变量,所以我不太清楚为什么我遇到了这个问题.
我正在尝试重载加法和乘法运算符(作为非成员函数),但它仍然试图使用它,好像我只允许有一个参数用于重载.
这是我正在做的一些代码:
1.未识别的变量
/* Quadratic.h */
#include <iostream>
using namespace std;
class Quadratic
{
public:
// constructors
Quadratic::Quadratic()
// accessors
Quadratic::getA();
Quadratic::getB();
Quadratic::getC();
// mutators
Quadratic::setCoefficients(float coA, float coB, float coC);
private:
float a, b, c, x;
};
Run Code Online (Sandbox Code Playgroud)
Quadratic.cpp:
/* Quadratic.cpp */
#include <iostream>
#include "Quadratic.h"
using namespace std;
class Quadratic
{
// Default constructor
Quadratic::Quadratic()
{
a = 0; // Visual Studio is complaining about a, b, & c
b = 0;
c …Run Code Online (Sandbox Code Playgroud) 以下代码演示了简单的运算符重载:
类位置有3个int字段和一个打印方法.
class Position
{
private:
int x,y,z;
public:
void print()
{
cout << x << "," << y << "," << z << endl;
}
Position (int a, int b, int c)
: x(4),
y(50),
z(23)
{
x=a;
y=b;
z=c;
}
Position operator+(const Position &other);
Position operator*=(const int &multi);
};
Run Code Online (Sandbox Code Playgroud)
运算符+和*=如此重载:
Position Position::operator+ (const Position &other)
{
x = x + other.x;
y = y + other.y;
z = z + other.z;
return *this;
}
Position Position::operator*= (const int …Run Code Online (Sandbox Code Playgroud) 我有一个像这样定义的typedef结构:
typedef struct myStruct {
int id;
double value;
bool operator <(const myStruct &x, const myStruct &y) {
return (x.id < y.id);
}
} myStruct;
Run Code Online (Sandbox Code Playgroud)
我需要将此结构用作std :: map中的键,从而重载运算符.但是,我在尝试编译时收到以下错误消息:
overloaded 'operator<' must be a binary operator (has 3 parameters)
Run Code Online (Sandbox Code Playgroud)
好的,所以我尝试了这个:
bool operator <(const pointcloud_keyframe &x) {
return (this->id < x.id);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为我在尝试插入地图时收到此错误消息:
invalid operands to binary expression ('const myStruct' and 'const myStruct')
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我有两个不同长度的 C++ 字符串。
string str1 = "01001110 01100001 01101101 01100101"
string str2 = "00000000 00000000 00000011"
Run Code Online (Sandbox Code Playgroud)
我需要异str1或str2并将结果存储在一个新字符串中
string xor_string = str1 ^ str2
Run Code Online (Sandbox Code Playgroud)
那么,是否可以将xor两个不同长度的字符串存储到另一个字符串中?
我已经搜索过了,但没有找到。
谁能告诉我,怎么做?
我试图覆盖类的==运算符,但是比较似乎以某种方式失败。例如,当我编写与称为eq的函数相同的代码时,不会发生任何问题。
class geo
{
...
bool operator==(geo const& other)
{
if(_id != other._id) return false;
if(!(name == other.name))return false;
if(is_primary!=other.is_primary)return false;
for(int i = 0; i<6;i++){
if(position[i]!=other.position[i])
return false;
}
return true;
}
....
private:
int _id, is_primary;
vector position;
string name;
}
Run Code Online (Sandbox Code Playgroud)
主要功能:...
geo* i= new geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);
geo* j= new geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);
if(i==j)
std::cout<<"they are equal\n";
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此命令时,它表示i和j有所不同。知道我在哪里做错事了吗?
编辑:谢谢盖兹的评论。我刚刚解决了;上面显示的代码可以正常工作。当然,如果我正在尝试简化代码,以使此处粘贴可读性。因此,我正在更新上面的代码以使其成为问题,以便将来的读者可能会看到比我更好的解决方案,同时我可以学到更多。
有人可以解释std::ostream &out这个朋友重载运算符函数的目的吗?为什么不直接std::cout在函数内部使用?
friend std::ostream& operator<<(std::ostream& out, const Complex& c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
Run Code Online (Sandbox Code Playgroud)
相反,像这样:
friend operator<<(const Complex& c)
{
std::cout << c.real;
std::cout << "+i" << c.imag << endl;
}
Run Code Online (Sandbox Code Playgroud) 我已经定义了一个Player类来进行一些操作,所以我可以方便地重载一些基本操作符.具体来说,我想使用<用于Player对象之间的比较.因此,我在课堂上有以下内容:
bool operator<(const Player& rhs) const {return (*this < rhs );}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这导致了问题.后来,当我尝试在main函数中输出包含特定元素的向量时,编译器让我知道<< operand没有匹配,并且它需要std :: ostream << Player.以下是导致问题的一行:
vector<Player> playerVec(6);
for (int i = 0; i < 6; i++) {
cout << playerVec[i];
}
Run Code Online (Sandbox Code Playgroud)
请注意,我实际上并不想直接输出任何Player对象流,所以我认为我不需要重载<<.
我对发生的事情有一些了解,因为编译器采用了我的特定定义<然后不打算寻找更一般的情况.我的问题是,我现在需要重载<<运算符以返回其一般功能,还是有更简单的解决方案?
感谢您提供的任何帮助!
我有运算符重载的问题.我有一个名为的类Point1,定义为
class Point1 {
private:
long double x;
public:
Point1(): x(0) {}
Point1(long double val): x(val) {}
Point1(Point1 & val): x(val.x) {}
//Some functions omitted
friend ofstream& operator<< (ofstream&, const Point1&);
friend ifstream& operator>> (ifstream&, Point1&);
};
Run Code Online (Sandbox Code Playgroud)
这个类正在工作,为其执行operator>>(ifstream&, Point1&);,其功能主体是:
double tmp;
in >> tmp; //In this line g++ breaks with an error
pnt.x=tmp;
return in;
Run Code Online (Sandbox Code Playgroud)
我在debian测试中使用gcc 4.9.3(armv7l).完整的源代码可以在这里找到:http://hastebin.com/igunaquxiw.cpp