【发布时间】:2017-04-10 20:22:11
【问题描述】:
需要一些家庭作业帮助。我是 C++ 新手,遇到了一个我不明白的错误。这是我的代码:
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main () {
void welcomeScreen();
void printLine( int length );
welcomeScreen();
return 0;
}
void welcomeScreen() {
string userName = "";
string title1 = "CMPSC101 FINAL PROJECT";
string title2 = "CREATED BY: OMEGA9380";
// Welcome screen:
system("CLS");
cout << "/";
printLine(80);
cout << "\\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (title1.length() / 2)) << title1 << setw(40 - (title1.length() / 2)) << "|" << endl;
cout << "|" << setw(41 + (title2.length() / 2)) << title2 << setw(40 - (title2.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\\";
printLine(80);
cout << "/" << endl;
}
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
错误是“错误:'printLine' 未在此范围内声明”。我确实在 main() 函数中声明了“printLine()”,这还不够吗?或者我是否需要在我计划使用的每个函数中声明函数名?为了回答这个紧迫的问题,我必须在这个最终项目中使用函数。谢谢!!!
【问题讨论】:
-
添加前向声明或上移你的函数。
-
更容易在任何其他函数之外声明您的函数。
-
关键是“在这个范围内”
void printLine( int length );被声明在main内,所以它只在main的范围内可见。
标签: c++ function scope declare