我正在尝试打印 const 的值,但它不起作用。多年后我正在回归 C++,所以我知道转换是一个可能的解决方案,但我也无法让它工作。
代码如下:
//the number of blanks surrounding the greeting
const int pad = 0;
//the number of rows and columns to write
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl << "Rows : " + rows;
我试图打印“行”的值但没有成功。
最佳答案
你想要:
cout << endl << "Rows : " << rows;
请注意,这与 const
无关 - C++ 不允许您使用 +
运算符连接字符串和数字。你实际上在做的是那个神秘的东西,叫做指针运算。
关于C++ 常量转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6256197/