简体   繁体   English

不寻常的声明错误

[英]Unusual Declaration Error

This seems like a really easy fix, but I can't understand what it's coming from. 这似乎是一个非常简单的解决方案,但我无法理解它的来源。

Any help fully appreciated! 任何帮助完全赞赏!

The following 2 lines of code produce the following errors respectively. 以下两行代码分别产生以下错误。

vector <spades::player> players(4, player());
vector <spades::card> deck(52,card());

error: 'player' was not declared in this scope
error: 'card' was not declared in this scope

Below is my card.cpp 下面是我的card.cpp

#include <iostream>
#include <sys/ioctl.h>
#include <cstdio>
#include <unistd.h>
#include <locale.h>
#include <ncursesw/ncurses.h>

#include "card.h"
namespace spades {
card::card()
{
    cardSuit = 0;
    cardNum = 0;
}

card::card(int suit, int number)
{
    cardSuit = suit;
    cardNum = number;
}
}

Below is my player.cpp 下面是我的player.cpp

#include <iostream> // Stream declarations
#include <vector> //Vectors used to store deck and players hands
#include <string> //String declarations
#include <algorithm> //Shuffle Method
#include <sys/ioctl.h>
#include <cstdio>
#include <unistd.h>
#include <locale.h>
#include <ncursesw/ncurses.h>
#include "player.h"
namespace spades {
using namespace std;


player::player() {
    score =0; //total score
    bid = NULL; //bid for that round
    tricksTaken = 0; //score for thast round
    sandBag = 0; //the number of points you win, more than what you bid, every 10th bag = -100
    doubleNil = false;
    for(int i=0; i<13; i++)
        hand.push_back(card());
}

void player::addCard(spades::card b){
    for (int i=0; i<hand.size(); i++){
        //compare card being played to the ones in your hand to search and determine which one to erase
        if((hand.at(i).getCardNum() == 0) &&
            (hand.at(i).getSuit() == 0))
        {
            hand.at(i).setCardNum(b.getCardNum());
            hand.at(i).setSuit(b.getSuit());
            return;
        }
    }
}

void player::removeCard(spades::card a) {
    for (int i=0; i<hand.size(); i++){
        //compare card being played to the ones in your hand to search and determine which one to erase
        if((hand.at(i).getCardNum() == a.getCardNum()) &&
            (hand.at(i).getSuit() == a.getSuit()))
        {
            hand.at(i).setCardNum(0);
            hand.at(i).setSuit(0);
            return;
        }
    }
}
}

The compiler is actually complaining about the arguments you pass to vector constructors. 编译器实际上抱怨传递给向量构造函数的参数。 You specified player() and card() in the constructor arguments, while it is obvious that your types are actually named spades::player and spades::card . 你在构造函数参数中指定了player()card() ,而很明显你的类型实际上是名为spades::playerspades::card You correctly specified the spades:: part in template parameters. 您在模板参数中正确指定了spades:: part。 Why did you omit the spades:: part from the constructor arguments? 为什么从构造函数参数中省略了spades:: part?

It should be 它应该是

vector <spades::player> players(4, spades::player());
vector <spades::card> deck(52, spades::card());

It should be noted though that the explicit argument is unnecessary, so you can just do 应该注意的是,显式参数是不必要的,所以你可以这样做

vector <spades::player> players(4);
vector <spades::card> deck(52);

and get the same result. 并得到相同的结果。

Also you don't need the 你也不需要

    namespace spades {

    }

block in player.cpp, only around you class definition in the header file. 在player.cpp中阻止,只在头文件中的类定义周围。 Maybe you ment 也许你了

    using namespace spades;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM