Edogmonkey
Edogmonkey

Reputation: 55

VS compiler errors for template classes

VS is throwing weird compiler errors for this code, giving me 3 errors all on line 9. I've used code similar to this in other projects before and it has worked fine. The Node class is included in the header and both pointers are set to nullptr in the constructor.

template<class T>
class Edge
{
public:
    Edge<T> *next;
    Node<T> *destination;
    Edge<T>();
    ~Edge();
};

error C2143: syntax error: missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'

I assume that the last 2 errors are somehow related to the first, so I'm guessing there's one small syntax problem that' causing all 3 problems. Like I said, I've done stuff similar to this before with no issues so this is very confusing to me.

Upvotes: 0

Views: 30

Answers (1)

Beginner
Beginner

Reputation: 5457

Did you declare Node? The compiler needs to know about node before Edge.

This compiles:

#include <iostream>
using namespace std;

// Use a forward-declaration of Node, so that the compiler knows this type exists.
template<class T> class Node;

template<class T>
class Edge
{
public:
    Edge<T> *next;
    Node<T> *destination;
    Edge<T>(){};
    ~Edge(){};
};

int main() {
    Edge<int> test;
    std::cout<<&test<<std::endl;
    return 0;
}

Success time: 0 memory: 15240 signal:0

0x7ffd75720b70

Upvotes: 2

Related Questions