pair in c++

H

hpsoar

can anybody tell me what is wrong with the following class:
#include <iostream>

class GameObject {
public:
virtual ~GameObject() = 0;
virtual void print() = 0;
protected:
std::string name;
pair<int, int> position;
};

GameObject::~GameObject() {
std::cout << "Game object destructed!" << std::endl;
}
the error message is:
ISO C++ forbids declaration of `pair' with no type
when I add "using namespace std" before the class it get right
 
S

SG

[...]
        pair<int, int> position;
[...]
the error message is:
ISO C++ forbids declaration of `pair' with no type
when I add "using namespace std" before the class it get right

The compiler doesn't know that with "pair" you're referring to a type
because it hasn't seen a declaration of "pair". You need to include
the header <utility> to be able to do that.

http://www.cplusplus.com/reference/std/utility/

Cheers!
SG
 
S

SG

[...]
class GameObject {
[...]
        pair<int, int> position;
[...]
the error message is:
ISO C++ forbids declaration of `pair' with no type
when I add "using namespace std" before the class it get right

By "it get right" do you mean that it worked?

Well the class template "pair" is defined in the namespace std in
header <utility>. So yes, you have to do something about it to address
that. For example:

std::pair<int,int> position;

This is in fact what you SHOULD do in header files.

Cheers!
SG
 
H

hpsoar

[...]
pair<int, int> position;
[...]
the error message is:
ISO C++ forbids declaration of `pair' with no type
when I add "using namespace std" before the class it get right

The compiler doesn't know that with "pair" you're referring to a type
because it hasn't seen a declaration of "pair". You need to include
the header <utility> to be able to do that.

http://www.cplusplus.com/reference/std/utility/

Cheers!
SG

got it, thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top