我仍然不明白动机.
为什么他们分别为整数和浮点数乘法制作了两个不同的运算符(*和*.),就好像它们害怕超载一样,但同时它们曾经*用来表示类型的笛卡尔积?
type a = int * int ;;
Run Code Online (Sandbox Code Playgroud)
为什么他们突然变得如此勇敢?为什么不写
type a = int *.. int ;;
Run Code Online (Sandbox Code Playgroud)
或者其他的东西?
是否存在一些关系,这使得笛卡尔积更接近整数乘积并且更远离浮动乘积?
我想重载A的'+'运算符,struct但我得到编译器警告这是我的尝试:
struct wektor{
int x;
int y=0;
int norm(){
return x*x+y*y;
}
};
wektor& operator +(wektor &a,wektor &b){
wektor c;
c.x=a.x+b.x; // 12 line - warning here
c.y=a.y+b.y;
return c;
};
Run Code Online (Sandbox Code Playgroud)
编译器警告:
[警告]非静态数据成员初始化程序仅在-std = c ++ 11或-std = gnu ++ 11 [默认情况下启用] 12行中可用
我不经常涉及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) 我对C++很陌生,试图让这个简单的例子起作用,但由于某种原因,我得到了意想不到的结果.
#include<iostream>
using namespace std;
struct node{
string data;
node *next;
node *prev;
node() {}
bool operator==(const node &rhs) const {
return data == rhs.data;
}
};
int main(){
bool loop=true;
node* one = new node();
one->data = "oddly specific";
node* two = new node();
two->data = "Something else";
node* three = new node();
three->data = "oddly specific";
if (one == two)
cout << "one == two";
else
cout << "one != two";
if (one == three)
cout << …Run Code Online (Sandbox Code Playgroud) 在阅读此文章似乎运营商+是一元的.那个怎么样.根据我的理解,一元运算符是一个运算符,它不依赖于另一个变量来操作,如++ a或a--.变量'+'是如何一元的.我以为是二进制?如果有人能清楚这一点,我将不胜感激.
我有一个Queue的实现,需要编写一个赋值运算符重载。
Queue& Queue::operator= (const Queue& rhs){
if(this->head == rhs.head) return *this;
Queue * newlist;
if(rhs.head == NULL){
// copying over an empty list will clear it.
this->clear();
return * newlist;
}
newlist = new Queue(rhs);
cout << "made new queue" << endl;
cout << "new list : " << * newlist << endl;
return * newlist;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我离开此功能时,的内容newlist将不再可访问。operator =()函数应该看起来如何?
编辑:queue.h:
class Queue : public LinkedList {
protected:
unsigned maxSize;
public:
Queue(unsigned N = -1);
Queue(const Collection& collection, unsigned …Run Code Online (Sandbox Code Playgroud) struct Test {
int A = 0;
int B = 0;
};
Test* operator+(const Test *x, const Test *r) {
Test *test = new Test;
test->A = x->A + r->A;
test->B = x->B + r->B;
return test;
}
Run Code Online (Sandbox Code Playgroud)
为什么这不会工作并给予:
3 IntelliSense:非成员运算符需要具有类或枚举类型的参数
我正在尝试为一个std :: map实例重载operator [],并对GCC的编译错误感到非常困惑.
以下示例将无法编译:
typedef std::map< int*, int > mymap;
namespace std {
template <>
int & mymap::operator[]( const int* & k) {
return begin()->second;
};
};
Run Code Online (Sandbox Code Playgroud)
这个失败了:
错误:对于'int&std :: map,std :: allocator >> :::运算符[](const int*&)'的模板ID'运算符[] <>'与任何模板声明都不匹配
但如果你int*用myintp(typedef int* myintp)替换它会编译得很好.
有趣的是,为什么这里需要模板<>和命名空间.
更新: 我简化了示例.
只有当声明取决于用户定义的类型且专业化满足原始模板的所有要求时,才允许将任何标准库模板的模板特化添加到命名空间std,除非禁止此类特殊化.
typedef std::map< myclass*, int > mymap;
namespace std {
template <>
int & mymap::operator[]( myclass* const & k) {
return begin()->second;
};
};
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)
请帮忙!
假设我正在使用一个结构在堆上分配并用作
然后我可以简单地重载它的新数组运算符,不要触摸任何delete []运算符
struct alignas(256) MyStruct
{
Item i1,i2;
void * operator new[](unsigned long int size)
{
return aligned_alloc(256,size);
}
void * operator new (unsigned long int size)
{
return aligned_alloc(256,size);
}
};
Run Code Online (Sandbox Code Playgroud)
并认为它没有任何泄漏?
GCC 6.3和c ++ 0x.