【问题标题】:C++ Getting an error with my functionsC++ 在我的函数中出现错误
【发布时间】: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


【解决方案1】:

假设你真的不想在 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;
}

【讨论】:

  • 修复了它。我没有意识到你可以在 main() 之外声明变量和函数。谢谢!!
  • @JeremyKellogg 当然可以!欢迎您的朋友,如果您有任何问题,请随时与我联系,我很乐意为您提供帮助。
【解决方案2】:

你的 printLine 函数只在 main 中声明,所以welcomeScreen 函数看不到它。

您应该将welcomeScreen 和printLine 函数移到main 之前,并确保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;
}

【讨论】:

    【解决方案3】:

    您在main() 函数的范围内声明了printLine() 函数。 welcomeScreen() 的定义看不到这个声明。

    printLine 的声明移到main 之外和welcomeScreen 之前

    welcomeScreen的声明也应该这样做

    【讨论】:

    • 您的回答是正确的。它只是有改进的余地。我不知道为什么它被否决了。
    • 我也不是,但你能做什么:) 我看到任何回答这个问题的人都会被否决
    • 这里有一个 up,你已经努力回答并被投票给 lul。猜猜它不满足 OP 什么的。
    【解决方案4】:

    你的函数没有声明,所以像这样使用它:

    /*
     * 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;
    }
    

    【讨论】:

    • 帮助别人得到-1,你们这些人太不可思议了。
    猜你喜欢
    • 2020-09-18
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多