当我编译该程序时,我得到错误消息:变量或字段为“绿色”;宣告无效。我有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/