我打算在调用时调用函数m_logger<<"hello"<<"world".m_logger属于ofstream类型.
所以我决定用以下签名重载<<
friend ofstream& operator<<(ofstream &stream,char *str);
Run Code Online (Sandbox Code Playgroud)
但是vc编译器会出现以下错误:
错误C2666:'operator <<':6次重载具有类似的转换
有没有其他方法可以实现这一点,我的目标是将所有写操作转移到ofstream对象到不同的功能?
创建我自己的calss对象对我有用,但是我怎样才能使它像普通的ofstream对象一样工作,它将所有系统定义的类型转换为字符串或char*.我知道一种方法是为每种类型的操作员重载但是有一种更清洁的方法
c++ operator-overloading operators visual-c++ operator-keyword
我的问题:我正在尝试重写赋值运算符以指向两个不同的类.这是一个例子:
dc.h:
#ifndef DC_H_
#define DC_H_
#include "ic.h"
class dc {
double d;
char c;
public:
dc(): d(0), c(0) { }
dc(double d_, char c_): d(d_), c(c_) { }
dc* operator=(const ic* rhs);
~dc() { }
};
#endif /* DC_H_ */
Run Code Online (Sandbox Code Playgroud)
class ic.h:
#ifndef IC_H_
#define IC_H_
class ic {
int i;
char c;
public:
ic(): i(0), c(0) { }
ic(int i_, char c_): i(i_), c(c_) { }
~ic() { }
};
#endif /* IC_H_ */
Run Code Online (Sandbox Code Playgroud)
dc.cpp:
#include "dc.h"
dc* …Run Code Online (Sandbox Code Playgroud) 首先要做的事情是:我知道这段代码过长,可能会缩短很多.但是,我不想要如何缩短它的帮助,我只是想了解一些基础知识,我现在的问题是运算符和存储值.正如您可以从代码中看到的那样,我试图使用一堆if语句在变量中存储特定值,然后在结尾处将这些值一起显示在字符串中.编译器不喜欢我的代码,并且给了我一堆与运算符相关的错误.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type = "";
string color = "";
string invTypeCode = "";
string invColorCode = "";
string fullCode = "";
cout << "Use this program to determine inventory code of furniture.";
cout << "Enter type of furniture: ";
cin >> type;
if (type.length() == 1)
{
if (type == "1" or "2")
{
if (type == "1")
{
invTypeCode = "T47" << endl;
}
if (type == "2")
{ …Run Code Online (Sandbox Code Playgroud) 我正在重载operator >>以从文件中读取一些类的变量.
我有这个奇怪的问题,很显然,它的工作原理为QString,QStringList但不为int!我已经尝试宣告我int的qint16,我也收到同样的错误消息.
.h 包含:
enum Anyo { Primero, Segundo, Tercero, Cuarto, Quinto, Sexto, ANYOS };
class Asignatura
{
public:
Asignatura();
Asignatura(const QString & nom, Anyo a, int hsCat);
friend QDataStream& operator<<(QDataStream &out, const Asignatura &a);
friend QDataStream& operator>>(QDataStream &in, Asignatura &a);
...
private:
static int idGeneral;
int id;
QString nombre;
QString nombreProfe;
Anyo anyo;
int hsCatedra;
int hsResueltas;
bool tieneProfe;
};
Run Code Online (Sandbox Code Playgroud)
.cpp 包含:
QDataStream& operator >>(QDataStream &in, …Run Code Online (Sandbox Code Playgroud) 我试图在一个课程作品上展示多样性,并希望使用<<运算符轻松地将变量添加到列表中.例如:
UpdateList<string> test;
test << "one" << "two" << "three";
Run Code Online (Sandbox Code Playgroud)
我的问题是,每个<<运算符的例子都与ostream有关.
我目前的尝试是:
template <class T> class UpdateList
{
...ect...
UpdateList<T>& operator <<(T &value)
{
return out;
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我是如何实现这一点的,或者在C++中实际上是不可能的?
我有一个格式的列表
['Jan 01', 'Feb 02', 'Mar 05', 'Feb 04', 'Jan 05', ...]
Run Code Online (Sandbox Code Playgroud)
我想对它进行排序
['Jan 01', 'Jan 05', 'Feb 02', 'Feb 04', 'Mar 05'...]
Run Code Online (Sandbox Code Playgroud)
我知道的事情operator.itemgetter('date'),但不认为这将适用于这种格式
#include <stdio.h>
#define max(x,y)(x)>(y)?x:y
int main() {
int i = 10;
int j = 5;
int k = 0;
k == max(i++, ++j);
printf("%d%d%d ", i, j, k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道答案.这是11 7 0怎么回事?请帮我执行三元运算符.
有没有办法在Swift中缩写以下类型的条件?
if ( (myEnum == .1 || myEnum == .2 || myEnum == .3 || myEnum == .8) && startButton.isSelected )
Run Code Online (Sandbox Code Playgroud)
我试着输入:
if ( (myEnum == .1 || .2 || .3 || .8) && startButton.isSelected )
Run Code Online (Sandbox Code Playgroud)
和:
if ( (myEnum == .1,.2,.3,.8) && startButton.isSelected )
Run Code Online (Sandbox Code Playgroud)
但这些都没有奏效.我也试过看文档,但找不到类似的例子.
谢谢!
我相信每个人都知道在csharp中带有'或'的if子句:
bool bananaIsYellow = true;
bool bananaIsBrown = false;
if (bananaIsYellow || bananaIsBrown) bool bananaIsRipe = true;
Run Code Online (Sandbox Code Playgroud)
事情是,一旦你开始比较字符串,这会变得非常混乱:
string bananaColor = yellow;
if (bananaColor == "yellow" ||
bananaColor == "brown" ||
bananaColor == "blue")
{
bool bananaIsRipe = true;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法缩短这个?
我知道的唯一方法就是这样(显然不是更漂亮或更短):
string bananacolor = "yellow";
if (StringComparer(bananacolor, new string[] { "yellow", "brown", "blue" })) { bool bananaIsRipe = true; }
}
private static bool StringComparer(string source, string[] values)
{
foreach (var value in values)
{
if (source == value) …Run Code Online (Sandbox Code Playgroud) 我正在编写代码来检查句子是否是回文.我觉得我的逻辑是正确的,但用于检查字符串反向的if语句似乎不起作用.我在网上尝试了很多解决方案,但仍然没有.每根弦都被视为不是回文.我觉得错误是在(句子==反向):
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]) {
while (true) {
string sentence;
string reverse = "";
cout << "Enter a sentence below to check (-1 to end):" << endl;
getline(cin, sentence);
if (sentence == "q") {
break;
}
for (int i = sentence.length(); i >= 0; i-- ) {
reverse += sentence[i];
}
cout << sentence << " reversed is: " << "[" << reverse << "]" …Run Code Online (Sandbox Code Playgroud) operator-keyword ×10
c++ ×5
if-statement ×2
string ×2
c ×1
c# ×1
date ×1
enums ×1
list ×1
macros ×1
operand ×1
operators ×1
overloading ×1
python ×1
qt ×1
sorting ×1
swift ×1
templates ×1
ternary ×1
visual-c++ ×1