expected Unqualified-id

Mar 14, 2012 at 5:20am
Hi all,

I need some help with my code - I've been trying to figure this out for hours. So I have a static int in my .h class, and I'm using a template<typename TItem> so that my code is parameterizable. I keep getting an error telling me that I'm supposed to have an unqualified id. Here's the relevant portion of my code:


//Node.cpp

#include <string>
#include "Node.h"


template <typename TItem> Node<TItem>::Node(){
next = NULL;
prev = NULL;
items = new TItem[capacity];
numItems = 0;
}

//.... etc ....

template <typename TItem> typename Node<TItem>::capacity = 0;

//That was my .cpp file. Here's my .h file


/Node.h

#pragma once
#include <string>
#include "Student.h"

using std::string;

template <typename TItem>

class Node{

static int capacity;

private:
Node* next;
Node* prev;
TItem* items;
int numItems;

//etc.

I have figured out that i'm supposed to have something before = in my .cpp file, but what is it?

Here's the error message:
Node.cpp:111:58: error: expected unqualified-id before ‘=’ token

I'm using the g++ compiler in ubuntu.

Other things that I've tried:
Put int in front of template in the .cpp file (i.e.

int template typename Node<TItem>::capacity = 0;

Which gives me the error

Node.cpp:111:5: error: expected unqualified-id before ‘template'

And so on. I've tried nearly every iteration of code that I can think of. I'm kind of new to C++, though, so I'm stumped.

Thanks for your help!
Mar 14, 2012 at 5:39am
Wrapping up your code in code tags is a good habit you know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


//Node.cpp

#include <string>
#include "Node.h"


template <typename TItem> Node<TItem>::Node(){
next = NULL;
prev = NULL;
items = new TItem[capacity];
numItems = 0;
}

//.... etc ....
///I think your problem is somewhere around here. Try replacing
template <typename TItem> typename Node<TItem>::capacity = 0;
///with
template <typename TItem> 
int Node<TItem>::capacity = 0;



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

//That was my .cpp file. Here's my .h file


/Node.h

#pragma once
#include <string>
#include "Student.h"

using std::string;

template <typename TItem>

class Node{

static int capacity;

private:
Node* next;
Node* prev;
TItem* items;
int numItems;

//etc. 

Mar 14, 2012 at 5:44am
OK, well it compiles. I'll go from there. Thanks! And I'll try to use code tags in the future... it's my first post, I'm getting the hang of things.
Topic archived. No new replies allowed.