【问题标题】:variable or field 'name of var' declared void声明为 void 的变量或字段“var 的名称”
【发布时间】:2017-02-27 03:28:05
【问题描述】:

当我编译这个程序时出现错误:变量或字段'push green;宣布无效。我有 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;
 }

【问题讨论】:

  • 当你编译这个程序时你会得到一个编译错误,而且你没有准确地引用它:至少不清楚错误结束的地方和你自己的话继续。

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


【解决方案1】:

在底部,您缺少一些括号。试试这个:

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

【讨论】:

    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多