得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
Getting error "left of '.toString' must have class/struct/union", even though there should be a fitting object left of toString
我必须为游戏Skat的某些方面编程。 了解游戏/游戏规则对于任务来说并不重要。(我也不知道。
我还没有完成程序,只是想运行一个简单的测试,但已经收到一个错误,我似乎无法修复。基本上,我创建了一个名为"one"的"SkatGame"类的新对象,然后我想将"一"的"套牌"中的所有牌输出到控制台。但是这不起作用,并且错误似乎表明"一"不是类的对象,我不知道为什么。
我复制了我的整个代码,但通过旁边的注释突出显示了我认为很重要的重要部分(因为缺乏对更好工具的了解)。任何帮助将不胜感激。
主文件.cpp文件
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include "Header.h"
int main() {
SkatGame one();
one.toString(); //error in this line
}
标题.h-文件
#pragma once
#include <string>
#include <array>
enum Suit { KARO = 0, HERZ, PIK, KREUZ };
enum Face { SIEBEN = 0, ACHT, NEUN, ZEHN, BUBE, DAME, KOENIG, ASS };
int const skatSize = 2;
int const deckSize = 32;
int const playerSize = 10;
class Card {
public:
Card();
Card(Suit, Face);
Suit getSuit();
Face getFace();
std::string toString();
private:
Suit suit;
Face face;
};
class SkatGame {
public:
SkatGame(); //!important line
void shuffle();
void deal();
std::string toString();
std::array <Card, skatSize> getSkat();
private:
std::array <Card, deckSize> deck;
std::array <Card, playerSize> player1;
std::array <Card, playerSize> player2;
std::array <Card, playerSize> player3;
std::array <Card, skatSize> skat;
};
详细的类定义文件 (.cpp)
#include "Header.h"
#include <iostream>
#include <string>
#include <array>
//#include <stdlib.h>
Card::Card() {};
Card::Card(Suit gesinnung, Face Zahl)
: suit(gesinnung), face(Zahl)
{};
Suit Card::getSuit() {
return Card::suit;
}
Face Card::getFace() {
return Card::face;
}
std::string Card::toString() { //!important part begin
std::string zurueck;
zurueck = std::to_string(Card::suit) + " " + std::to_string(Card::face);
return zurueck;
} //!important part end
SkatGame::SkatGame()
{
int a, b, e = 0;
for (a = 0; a <= 3; a++) {
for (b = 0; b <= 7; b++) {
SkatGame::deck[e] = Card(static_cast<Suit>(a), static_cast<Face>(b));
e++;
}
}
}
void SkatGame::shuffle() {
int a, card1, card2;
Card hold;
for (a = 0; a < 1000; a++){
card1 = rand() %32;
card2 = rand() %32;
hold = SkatGame::deck[card1];
SkatGame::deck[card1] = SkatGame::deck[card2];
SkatGame::deck[card2] = hold;
}
}
std::string SkatGame::toString() { //!important part begin
int i;
for (i = 0; i <= 31; i++) {
std::cout << (SkatGame::deck[i]).toString() << std::endl;
};
return "justatest";
} //!important part end
编辑:在声明期间删除一个后面的括号是有效的。 所以"SkatGame one;"而不是"SkatGame one();"。
你声明了一个函数'one',它不带参数并返回一个'SkatGame'。要称呼它,你写成'one() ;'
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- valgrind报告BSONObj::toString中未初始化的值,使用GENOID创建一个简单的BSON对象