我是C++的初学者,我有以下问题.当我在VS2013中运行以下代码时,出现错误.
class Y{
public:
Y(int un_x, int un_y)
: x_(un_x), y_(un_y) {}
int x() const {
return x_;
}
int y() const {
return y_;
}
private:
int x_;
int y_;
};
class X{
private:
Y coord;
public:
// some code ...
Y position() const {
return coord;
}
void display(ostream& output) const {
output << "The object is in position " << position();
}
};
ostream& operator<<(ostream& output, Y x){
output<< "(" << x.x() << ", " << x.y() …Run Code Online (Sandbox Code Playgroud) 我试图在C++中重载运算符.
我正在创建一个自定义Stack数据结构.
这是我Stack.h在headers目录中:
/*******************************************************************************
* Stack -- *
* This class will just interface a stack of integers. *
* A stack is a linear data structure. Elements are pushed into stack from the *
* bottom and is popped from the top. *
* *
* Author -- Aditya R.Singh *
* Version -- 1.0 *
* Since -- 2014-06-24 *
*******************************************************************************/
#ifndef STACK_H
#define STACK_H
class Stack {
public:
Stack(); // To initiallize stuff. …Run Code Online (Sandbox Code Playgroud) 我的程序不会编译,因为它没有找到操作数匹配.它访问struct Student中的地图,我不确定这是否是访问地图的确切方法.
我的程序不会编译,因为它没有找到操作数匹配.它访问struct Student中的地图,我不确定这是否是访问地图的确切方法.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
#include <list>
using namespace std;
struct Student {
string id;
map<string, int> scores;
};
istream& operator >>(istream &is, Sudent& g) {
auto it = g.scores.begin();
is >> g.id >> it->first >> it.second;
return is;
}
Run Code Online (Sandbox Code Playgroud)
在>> it->first我得到这个错误:
Error: no operator ">>" matches these operands
operand types are: std::basic_istream<char, std::char_traits<char>> >> const std::string
Run Code Online (Sandbox Code Playgroud) 我有一个名为 的类Fstring,其中有一个wchar_t*。
我写了以下内容将字符串文字复制到Fstring:
#include <iostream>
using namespace std;
class Fstring{
wchar_t *arr;
public:
Fstring& operator = (const wchar_t temp[])
{
delete [] arr;
arr=new wchar_t[wcslen(temp)];
for(int i=0;i<=wcslen(temp);i++)
arr[i]=temp[i];
return *this;
}
};
int main()
{
Fstring test=L"Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它没有用。编译器给了我以下错误:
错误 C2440:“正在初始化”:无法从“const wchar_t [12]”转换为“Fstring”
我真的很困惑,我在谷歌上搜索了“重载运算符”,但所有结果都与我用来重载运算符的方式相同。那么为什么这不起作用呢?
我正在写作,因为我想了解一些关于C++设计的东西.
问题如下:在C++中,可以通过传递两个rhs值来重载类操作符.但是无法获取有关应用这些操作的输出的信息.
例如,想想Matrix类的实现:
相关代码看起来像
template <typename DataType, int NumberOfRows, int NumberOfColumns>
class Matrix
{
DataType _data[NumberOfRows * NumberOfColumns];
public:
typedef DataType TDataType;
Matrix() {}
...missing code...
template <int SecondNumberOfColumns>
friend inline Matrix<DataType, NumberOfRows, SecondNumberOfColumns>
operator*(Matrix const& First,
Matrix<DataType, NumberOfColumns, SecondNumberOfColumns> const& Second)
{
Matrix<DataType, NumberOfRows, SecondNumberOfColumns> result; //HERE THE ALLOCATION OF A TEMP IS NEEDED
for (int i = 0; i < NumberOfRows; i++)
for (int j = 0; j < SecondNumberOfColumns; j++) {
DataType temp = …Run Code Online (Sandbox Code Playgroud) 我正在学习链表.我创建了一个模板实现,包括构造函数,插入器,析构函数,复制构造函数和重载赋值运算符.问题是我的测试程序在重载赋值运算符后没有输出任何内容.
对于我的赋值运算符,我使用Clear()函数在复制之前完全清除列表.我把它放在析构函数中并检查它是否正常工作.我还检查了我的复制构造函数,它也运行良好.
文件node.h:定义节点构建块
#include <iostream>
using namespace std;
template <typename T>
struct Node{
T _item;
Node<T>* _next;
Node() {
_item = T();
_next = NULL;
}
Node(T item){
_item = item;
_next = NULL;
}
// Print the value of a node
friend std::ostream& operator <<(std::ostream& outs, const Node<T> &printMe){
outs << "[" << printMe._item << "]";
return outs;
}
};
Run Code Online (Sandbox Code Playgroud)
文件list.h:定义链接列表模板
#include "node.h"
template <class T>
class List {
public:
// …Run Code Online (Sandbox Code Playgroud) 我已经进行了一段时间的 Go 之旅,我刚刚注意到这行代码:-
today := time.Now().Weekday()
switch time.Saturday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
Run Code Online (Sandbox Code Playgroud)
如果 Go 不支持运算符重载,我如何使用“+”运算符来增加一天?
我正在编写一个名为的类StringSet,它具有vector<string> data和int length作为其私有成员。
bool StringSet::operator == (StringSet d)
{
for (int i = 0; i < length; i++)
{
if (data[i] == d.data[i])
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样调用这个函数时,
StringSet doc1, doc2;
if (doc1 == doc2)
{
cout << "Both sentences are identical!\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到一个断言失败,说向量下标超出范围,我知道这意味着什么,但我不知道它在这里意味着什么。如果有人能指出我犯的一个明显错误,那就太好了,因为我是 C++ 的新手。
为什么c#不允许运算符重载而C++呢?
当我试图超载时,我收到了这个错误.
期望可重载二进制运算符
我的示例代码看起来像,
public static MyClass operator +=(MyClass obj1, MyClass obj2)
{
...
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
Java String.equals与==
是否可以使用==运算符比较Java字符串?
为什么我经常看到,使用equals()方法代替?
是因为在与文字字符串(如"Hello")比较时使用==并不意味着调用equals()?