Reputation: 87
So, I'm trying to make a text based game in C++ Using Visual Studio 2010. Here are some of the code blocks that I think are related. If you need anymore, don't hesitate to ask me.
I'm trying to create a class for a game called places. I make a place, and it has another "place" to the North, South, East, and West of it. I'm just really confused right now. I'm a noob at this stuff. I may just be looking over something.
//places.h------------------------
#include "place.h"
//Nowhere place
string nowheredescr = "A strange hole to nowhere";
place nowhere(&nowheredescr, &nowhere, &nowhere, &nowhere, &nowhere); //Error occurs here
//
//place.h------------------------
#ifndef place_h
#define place_h
#include "classes.h"
class place
{
public:
place(string *Sdescription, place *Snorth, place *Ssouth, place *Swest, place *Seast);
~place(void);
private:
string *description;
place *north;
place *south;
place *east;
place *west;
};
#endif
//place.cpp-------------------
#include "place.h"
#include <iostream>
place::place(string *Sdescription, place *Snorth, place *Ssouth, place *Swest, place *Seast)
{
description = Sdescription;
north = Snorth;
south = Ssouth;
west = Swest;
east = Seast;
}
place::~place(void)
{
}
Upvotes: 2
Views: 1094
Reputation: 8604
Following syntax would solve the error
place nowhere = place(&nowheredescr, &nowhere, &nowhere, &nowhere, &nowhere);
That is explained in C++03 standard, 3.3.1/1
The point of declaration for a name is immediately after its complete declarator (clause 8) and before its initializer (if any)
In OP example, place nowhere(.....)
represents a declarator, therefore nowhere
used as a constructor parameter is considered undeclared.
In my example, the place nowhere
is a declarator and place(.....)
is an initializer, therefore nowhere
becomes declared at that point.
Upvotes: 2