我遇到了谷歌无法解决的问题.为什么该cout适用于int对象,但不适用于以下程序中的字符串对象?
#include<iostream>
using namespace std;
class MyClass {
string val;
public:
//Normal constructor.
MyClass(string i) {
val= i;
cout << "Inside normal constructor\n";
}
//Copy constructor
MyClass(const MyClass &o) {
val = o.val;
cout << "Inside copy constructor.\n";
}
string getval() {return val; }
};
void display(MyClass ob)
{
cout << ob.getval() << endl; //works for int but not strings
}
int main()
{
MyClass a("Hello");
display(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我遇到了一个奇怪的问题.由于某种原因多个|| 语句,即使除以逗号和括号也不起作用.我期望工作的最后一件事是&语句,它要求满足两个条件,但在我的情况下,它适用于一个条件,就好像它是一个OR语句.
有人请向我解释为什么会这样.我很迷茫.
作品:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string quest;
quest = "Where is my dog?";
string::iterator m;
vector<string>question;
string t;
for(m = quest.begin(); m != quest.end(); m++)
{
if(*m != ' ' & *m != ',' & *m != '?' & *m != '.') //works with & and &&
{
t.push_back(*m);
}
else
{
cout << t << endl;
question.push_back(t);
t.clear();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不起作用:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{ …Run Code Online (Sandbox Code Playgroud)