2017-09-17 91 views
-1

我遇到的問題是當我嘗試在3個類對象的數組中設置一些類變量時,出現錯誤,指出該函數所需的變量不是在主要功能的範圍內聲明。未在主函數中聲明的類變量C++

我還是新來的類和分離我的接口從我的實現,我發現它有時混淆。

我一直在看這個網站和其他一些其他的例子,我無法弄清楚我的問題是什麼以及如何解決它。

interface.h文件:

#ifndef INTERFACE_H 
    #define INTERFACE_H 

    #include <iostream> 
    #include <string> 
    #include <fstream> 

    using namespace std; // I know this is a programming sin to include inside a header file but my textbook wants me to do it this way 

    class student 
    { 
    public: 
     student(); // class constructor 
     void setstudent(string studentName, string ID, string studentNumber, string diploma); // Function im using to set objects in array object 

    private: 
     string studentName; 
     string ID; 
     string studentNumber; // class variables 
     string diploma; 
     int averageMark; 
     string codes [5]; 
     int marks [5]; 
    }; 


    #endif // INTERFACE_H 

implementation.cpp文件:

#include <iostream> 
#include "interface.h" 

using namespace std; 

student::student() 
{ 
    studentName = ""; 
    ID = ""; 
    studentNumber = ""; // constructor to initialise all class variables 
    diploma = ""; 
    averageMark = 0; 
    codes [5] = {"", "", "", "", ""}; 
    marks [5] = {0, 0, 0, 0, 0}; 
} 

void student::setstudent(string studentName, string ID, string studentNumber, string diploma) // function to set class variables 
{ 
    cout << "Please enter a student name" << endl; 
    cin.getline(studentName); 

    cout << "Please enter a unique student ID" << endl; 
    cin.getline(ID); 

    cout << "Please enter a unique student number" << endl; 
    cin.getline(studentNumber); 

    cout << "Please enter diploma name" << endl; 
    cin.getline(diploma) 
    if (diploma == "Garden Design") 
    { 
     codes[5] = {"G1","G2","G3","G4","G5"}; 
    } 
    else if (diploma == "Gourmet Cooking") 
    { 
     codes[5] = {"C1","C2","C3","C4","C5"}; 
    } 
} 

的main.cpp文件:

#include <iostream> 
#include "interface.h" 

using namespace std; 

int main() 
{ 
    student studentDetails[3]; // Creation of class array object to store 3 objects/Maybe ive declared my array to hold 3 class objects incorrectly? 
    for (int i = 0; i < 3; i++) 
    { 
     studentDetails[i].setstudent(studentName, ID, studentNumber, diploma); // function call to set class variables for the objects 

    } 

    return 0; 
} 

如果任何人都可以請指出我所做錯了,並指出我在正確的方向將非常感激。

錯誤的代碼塊:

||=== Build: Debug in student (compiler: GNU GCC Compiler) ===| 
C:\Users\nicbe\Desktop\Student\student\main.cpp||In function 'int main()':| 
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentName' was not declared in this scope| 
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'ID' was not declared in this scope| 
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentNumber' was not declared in this scope| 
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'diploma' was not declared in this scope| 
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 
+1

'setstudent'不應該帶參數。 – tkausl

+2

你的'學生'構造函數不能按照你所期望的那樣工作。您定義一組*局部變量*並初始化它們。類成員變量不會被初始化。而你調用的成員函數,你傳遞的參數是不存在於'main'函數範圍內(或全局範圍內)的變量。還有很多其他問題,比如你試圖分配給數組。總而言之,你似乎可以使用[一些好的初學者書籍來閱讀](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –

+0

@tkausl是的,至少它已被聲明(和定義)採取參數。否則,錯誤會有所不同。 –

回答

0

錯誤正是它說:沒有宣佈。

在您的主要功能:

studentDetails[i].setstudent(studentName, ID, studentNumber, diploma); 

突然這四個參數是憑空出現的(?)。 不知道你是否剛剛排除那部分或什麼。