我正在尝试使用名为“Deck”的类创建程序。但是,我无法定义头文件之外的函数。
标题Deck.h文件具有以下内容:
#ifndef DECK_H
#define DECK_H
#include <vector>
//#include "Card.h"
class Deck{
public:
vector<Card>* deck = new vector<Card>;
bool visibility; //Whether you are allowed to view the deck
Deck();
Deck(bool visibility);
void shuffle();
};
#endif
Deck.cpp文件包含以下代码:
#include "Deck.h"
#include <vector>
#include "stdafx.h"
Deck::Deck(){
this.visibilty = false;
}
Deck::Deck(bool visibility){
this.visibility = visibility;
}
void Deck::shuffle(){
vector<Card>* shuffleHolder = new vector<Card>;
shuffleHolder = this.deck;
}
出于某种原因,我总是收到错误:
1>Compiling...
1>Deck.cpp
1>.\Deck.cpp(5) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Deck.cpp(6) : error C2673: 'Deck' : global functions do not have 'this' pointers
1>.\Deck.cpp(6) : error C2228: left of '.visibilty' must have class/struct/union
1>.\Deck.cpp(7) : warning C4508: 'Deck' : function should return a value; 'void' return type assumed
1>.\Deck.cpp(9) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Deck.cpp(10) : error C2673: 'Deck' : global functions do not have 'this' pointers
1>.\Deck.cpp(10) : error C2228: left of '.visibility' must have class/struct/union
1>.\Deck.cpp(11) : warning C4508: 'Deck' : function should return a value; 'void' return type assumed
1>.\Deck.cpp(13) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(14) : error C2065: 'vector' : undeclared identifier
1>.\Deck.cpp(14) : error C2065: 'Card' : undeclared identifier
1>.\Deck.cpp(14) : error C2065: 'shuffleHolder' : undeclared identifier
1>.\Deck.cpp(14) : error C2061: syntax error : identifier 'vector'
1>.\Deck.cpp(16) : error C2673: 'shuffle' : global functions do not have 'this' pointers
1>.\Deck.cpp(16) : error C2228: left of '.deck' must have class/struct/union
我试图找出问题所在,否则我可能必须定义Deck.h文件中的所有函数并完全删除.cpp文件。
答案 0 :(得分:0)
使用预编译头文件#include "stdafx.h"
必须出现在每个文件中的每个#include之前。否则预处理器/编译器将忽略其他文件。您可以阅读有关如何使用预编译标头here的更多信息。如果您不需要改进的编译时间,则另一个选项就是不使用它们。
关于您的代码的一些不相关的注释:this
是指针到当前对象,因此您无法使用点.
运算符(用于具有类类型的对象,而不是指针)。您必须使用->
。例如this->visibility
或者只有visibility
明确无误。此外,由于您使用new
进行动态内存分配,因此必须使用一个调用delete
的析构函数。但是在这种特殊情况下,new
不是必需的,您可以让向量通过使其成为向量变量而不是指针而不是使用new
来定义它来管理内存本身。