我的类在.h文件中定义,找不到.cpp文件

问题描述 投票:1回答:1

我正在尝试使用名为“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文件。

c++ visual-studio class
1个回答
0
投票

使用预编译头时,#include "stdafx.h"必须出现在每个文件中的每个#include之前。否则预处理器/编译器将忽略其他文件。您可以阅读有关如何使用预编译头here的更多信息。如果您不需要改进的编译时间,则另一个选项是不使用它们。

关于代码的一些不相关的注释:this是指向当前对象的指针,因此您无法使用点.运算符(用于具有类类型的对象,而不是指针)访问它的属性。你必须使用->。例如this->visibility或如果它是明确的visibility。此外,由于你使用new进行动态内存分配,你必须有一个调用delete的析构函数。但是在这种特殊情况下,不需要new,你可以让矢量通过使它成为一个矢量变量而不是一个指针而不是用new来定义它来管理内存本身。

© www.soinside.com 2019 - 2024. All rights reserved.