2017-02-27 74 views
-3

當我編譯這個程序時,我得到錯誤:變量或字段'推綠色;宣佈無效。我有2個文件:一個頭文件和一個cpp文件。不確定是什麼導致了這個錯誤。變量或字段'VAR的名稱'宣佈無效

/////thinker.h 
#include <cstring> 
#include <assert.h> 
#include <string> 

class thinking_cap 
{ 
    public: 
     void slots(char new_green[], char new_red[]); 
     void push_green() const; 
     void push_red() const; 

private: 
char green_string[50]; 
char red_string[50]; 
}; 



//////// thinker.cpp 
    #include <iostream> 
#include <stdlib.h> 
#include "thinker.h" 

int main() 
    { 
    thinking_cap student; 
    thinking_cap fan; 
    student.slots("Hello", "Goodbye"); 
    fan.slots("Go Cougars!", "Boo!"); 
    student.push_green(); 
    fan.push_green(); 
    student.push_red(); 
    return 0; 
} 

void thinking_cap::slots(char new_green[ ], char new_red[ ]) 
{ 
    assert(strlen(new_green) < 50); 
    assert(strlen(new_red) < 50); 
    strcpy(green_string, new_green); 
    strcpy(red_string, new_red); 
} 
void thinking_cap::push_green 
{ 
    cout << green_string << endl; 
} 
void thinking_cap::push_red 
{ 
    cout << red_string << endl; 
} 
+0

當你* *編譯這個程序,你得到一個編譯錯誤,而你還沒有準確地引用它:至少目前還不清楚其中的錯誤,並結束自己的話重新開始。 – EJP

回答

0

倒在最下面,你錯過了一些括號。試試這個:

void thinking_cap::push_green() const 
{ 
    cout << green_string << endl; 
} 
void thinking_cap::push_red() const 
{ 
    cout << red_string << endl; 
} 
相關問題