【发布时间】:2017-09-16 12:35:06
【问题描述】:
卡.h
#pragma once
#include <string>
#include "Rank.h"
#include "Suit.h"
using namespace std;
/**
*
*/
class MEMORYWARS_API Card
{
public:
Card(Rank, Suit);
string toString() const;
~Card();
private:
Rank rank;
Suit suit;
};
卡片.cpp
#include "MemoryWars.h"
#include "Card.h"
Card::Card(Rank rank, Suit suit)
{
this->rank = rank;
this->suit = suit;
}
string Card::toString() const
{
string s = "Hellow there";
return s;
}
Card::~Card()
{
}
错误
error C3867: 'Card::toString': non-standard syntax; use '&' to create a pointer to member
甲板.h
#pragma once
#include "Card.h"
#include "vector"
class MEMORYWARS_API Deck
{
public:
Deck();
~Deck();
private:
std::vector<Card> deck;
};
Deck.cpp
#include "MemoryWars.h"
#include "Deck.h"
#include <EngineGlobals.h>
#include <Runtime/Engine/Classes/Engine/Engine.h>
Deck::Deck()
: deck(52)
{
int cc = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 13; j++)
{
Rank rank = static_cast<Rank>(j);
Suit suit = static_cast<Suit>(i);
deck[cc] = Card(rank, suit);
cc++;
}
}
string st = deck[2].toString;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Some variable values: x: %s"), st));
}
Deck::~Deck()
{
}
我是 C++ 新手,主要是 Java 经验,我一直在努力解决这个错误。
我正在尝试测试 Card::toString 方法,但每次我从 deck.cpp 调用它时都会出错。
【问题讨论】:
-
string st = deck[2].toString();如果要调用函数,需要parens。 -
调用成员函数在 C++ 中和在 Java 中基本相同。
-
我很抱歉这是一个愚蠢的错误...facepalm 不管怎样,我更新了我的帖子。我开始遇到另一个错误,请您帮帮我。
-
Deck(52)尝试通过Card()构造52张卡片,但是没有这样的默认(读取:无参数)构造函数。定义默认构造函数或将Cards 存储为指针。 (在 Java 中,它们最初可能为 null,因为 Java 通过引用完成所有操作。在 C++ 中,对象或引用是必须明确区分的。) -
@alcedine 可能只想
deck.reserve(52)和deck.push_back(Card(rank, suit))
标签: c++ unreal-engine4