var的变量或字段名称'宣布无效

时间:2017-02-27 03:28:06

标签: c++ class compiler-errors header-files

当我编译这个程序时,我得到错误:变量或字段'推绿;宣布无效。我有2个文件:头文件和cpp文件。不确定导致此错误的原因。

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

1 个答案:

答案 0 :(得分:0)

在底部,你错过了一些括号。试试这个:

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