请问大家一个c++primer练习7.27的问题,按照习题答案这样写,提示我参数过多
#include <iostream>
#include <string>
#include<vector>
using namespace std;
class Screen
{
private:
unsigned height = 0, width = 0;
unsigned cursor = 0;
string contents;
public:
Screen() = default;
Screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht*wd, ' '){}
Screen(unsigned ht, unsigned wd, char c) :height(ht), width(wd), contents(ht*wd, c){}
public:
Screen& move(unsigned r, unsigned c)
{
cursor = r*width + c;
return *this;
}
Screen& set(char ch)
{
contents[cursor] = ch;
return *this;
}
Screen& set(unsigned r, unsigned c,char ch)
{
contents[r*width + c] = ch;
return *this;
}
Screen& display()
{
cout<< contents;
return *this;
}
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#').display(cout);
cout << endl;
myScreen.display(cout);
cout << endl;
}
};
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#').display(cout);
cout << endl;
myScreen.display(cout);
cout << endl;
}
这个应该写在类外面
myScreen.display(cout);
这个去掉括号里的cout
myScreen.move(4, 0).set('#').display(cout);
.display(cout);
不应该有参数的
#include
#include
#include
using namespace std;
class Screen
{
private:
unsigned height = 0, width = 0;
unsigned cursor = 0;
string contents;
public:
Screen() = default;
Screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht*wd, ' '){}
Screen(unsigned ht, unsigned wd, char c) :height(ht), width(wd), contents(ht*wd, c){}
public:
Screen& move(unsigned r, unsigned c)
{
cursor = r*width + c;
return *this;
}
Screen& set(char ch)
{
contents[cursor] = ch;
return *this;
}
Screen& set(unsigned r, unsigned c,char ch)
{
contents[r*width + c] = ch;
return *this;
}
Screen& display()
{
cout<< contents;
return *this;
}
};
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#').display();
cout << endl;
myScreen.display();
cout << endl;
return 1;
}
main函数在调用display函数是为什么有实参,而你的display的定义中没有参数。
首先,你的主函数应该放在类外。
其次,主函数中的display不应该有参数,因为你声明时就没有参数
谢谢大家的帮助,有你们太好了,刚开始学习c++类没多久,