c++ - 变量或字段 'name of var'声明为void

标签 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;
 }

最佳答案

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

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

关于c++ - 变量或字段 'name of var'声明为void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42477372/

相关文章:

c++ - 确定通过 malloc() 进行的分配是否由大页面支持

C++ 初始化类成员对象

c++ - 动态数组 C++ 多项式类

ruby - 'case' 语句如何使用常量?

java - Dijkstra 算法模拟

c++ - 错误编译 Unresolved external 错误

python - 编译Python项目Windows

c++ - 使用 Detours 3.0 Express 的简单数据包记录器

c++ - 继承运算符 +()

ios - Swift 中的静态属性