ERROR: 'Deck{ctor}' cannot have a return type

When trying to build a solution in MSVC, I keep getting this error, and I can't figure out what's wrong. I've looked and looked, but can't find any signs of a return type for the constructor. (But then again, I've had ~12 hours of sleep total this week and have been abusing caffiene to the point of criminality, so it's entirely possible that I'm just glossing over an otherwise blatantly obvious error :P )

Deck.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Deck
{
    private:
    enum {MAXSIZE=52};
    struct Card {std::string value; struct Card* next;};
    Card* first;
    Card* last;
    int cardCount;
    const int deckSize;
    
    public:
    Deck(int ds = MAXSIZE);
    //... 


Deck.cpp:
1
2
3
4
5
6
7
8
9
#include "deck.h"
#include <string>

Deck::Deck(int ds) : deckSize(ds)
{
    first = last = NULL;
    cardCount = 0;
}
//... 
Did you forget a semicolon after the class ending brace } ? Maybe <string> got included elsewhere so it didn't get included there, so it looks like:
1
2
3
4
5
class blah
{
    //
}
blah::blah(){}
where blah::blah() returns blah (due to the lack of a semicolon)
Last edited on
Correct me if im wrong, but on this line of code: : deckSize(ds) you are trying to assign a constant a value, which isnt possible. if you want to decalare the const int in the beggining of the program you would put static infront of the code, so as in: static const int deckSize = 52;. All i can see that might fix it.
Last edited on
I just compiled the code above with VS2010.
It worked for me (except for the error LNK2001: unresolved external symbol _main).

The problem is not with this part of the code.

A) How do you call the constructor.
B) do you have any other constructors?
@Need4Sleep: You need some sleep, friend. This is perfectly valid C++ to construct a constant like that.

Proof: http://ideone.com/IN7GN
Thanks for the correction L B, ignore that post of mine then.
Hmm, here's the full code, and yes, I also get the 'unresolved external sysbol _main' but that's only because I havent written it yet.

Deck.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef DECK_H_
#define DECK_H_
#include <string>

class Deck
{
    private:
    enum {MAXSIZE=52};
    struct Card {std::string value; struct Card* next;};
    Card* first;
    Card* last;
    int cardCount;
    const int deckSize;
    //prevent public copying
    Deck(const Deck& d) : deckSize(0) {}
    Deck& operator=(const Deck& d) {return *this;}

    public:
    Deck(int ds = MAXSIZE);
    ~Deck();
    bool isWinner() const;
    bool isEmpty() const;
    int countDeckSize() const;
    bool addCard( const std::string& str);
    bool playCard(std::string& str);
};
#endif 


Deck.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "deck.h"
#include <string>

Deck::Deck(int ds) : deckSize(ds)
{
    first = last = NULL;
    cardCount = 0;
}

Deck::~Deck()
{
    Card* temp;
    while(first != NULL)
    {
        temp = first;
        first = first->next;
        delete temp;
    }
}

bool Deck::isWinner() const
{
    return (cardCount == deckSize);
}

bool Deck::isEmpty() const
{
    return (cardCount == 0);
}

int Deck::countDeckSize() const
{
    return cardCount;
}

bool Deck::addCard(const std::string& str)
{
    if(isWinner())
        return false;
    Card* add = new Card;
    if(add==NULL)
        return false;
    add->value = str;
    add->next = NULL;
    cardCount++;
    if(first==NULL)
        first = add;
    else
        last->next = add;
    last = add;
    return true;
}

//this function is going to be altered, so ignore any warnings/errors originating here
bool Deck::playCard(std::string& str)
{
    if(first==NULL)
        return false;
    str = first->value;
    cardCount--;
    Card* temp = first;
    first = first->next;
    return true;

}
Last edited on
Copypaste the exact error message and tell us what line numbers it corresponds to.
Apparently, I made changes to the wrong .h file (I have a copy of the file on the desktop that I open in Notepad) and yes, the Deck.h file that was in the Visual Studio Project folder was missing the closing ';'.

Sorry I wasted your time for such a silly mistake.
I guess it's time for a quick nap

I tried this out and I have no compilation errors. If I add a main and define Deck Mydeck;. Everything works fine for me.

The fact that you're even getting a linking error tells us that the compiler finished its job successfully without errors. I'm a bit confused.
Last edited on
The code "works", but you have memory leaks in it and checking for NULL after operator "new" is called is useless in that form, in case of failure it will throw an exception and the program will end without going further.
@modoran: Where are the memory leaks, exactly? I thought that the destructor delete'ed all the pointers/freed all the memory that the class used.
Topic archived. No new replies allowed.