需要一些功课帮助。我是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()”,还不够吗?或者我是否需要在我计划使用的每个函数中声明函数名称?为了回答这个问题,我必须在最后的项目中使用函数。感谢!!!
答案 0 :(得分:3)
假设你真的不想在main中声明函数,你需要转发声明welcomeScreen和printLine函数:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int printLine(int);
void welcomeScreen();
int main () {
welcomeScreen();
return 0;
}
void welcomeScreen() {
// definition
}
void printLine(int length) {
// definition
}
或者只是在main之前定义它们:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int printLine(int length) {
//definition
}
void welcomeScreen() {
// definition
}
int main () {
welcomeScreen();
return 0;
}
答案 1 :(得分:2)
您的printLine函数仅在main中声明,因此welcomeScreen函数无法看到它。
您应该在main之前移动welcomeScreen和printLine函数,并确保printLine在welcomeScreen之前,以便welcomeScreen在您尝试调用它之前知道它存在。
像这样:
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
void welcomeScreen(void) {
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;
}
int main () {
welcomeScreen();
return 0;
}
答案 2 :(得分:1)
您在printLine()
函数范围内声明了main()
函数。 welcomeScreen()
的定义未见此声明。
将printLine
的声明移至main
之外且welcomeScreen
之前
同样应该使用welcomeScreen
答案 3 :(得分:-1)
您没有声明您的功能,因此请按照以下方式使用:
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
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 << "=";
}
}
int main () {
void welcomeScreen();
void printLine( int length );
welcomeScreen();
return 0;
}