C ++使用我的函数出错

时间:2017-04-10 20:22:11

标签: c++ function scope declare

需要一些功课帮助。我是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()”,还不够吗?或者我是否需要在我计划使用的每个函数中声明函数名称?为了回答这个问题,我必须在最后的项目中使用函数。感谢!!!

4 个答案:

答案 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;
}