编辑:
原始错误是我在构建之前遇到的Intellisense错误。建成后,我得到了更多的支持:
这些与Option.cpp中的这段代码有关:
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room);
return room;
}
Option.hpp中的相应代码:
public:
virtual shared_ptr<Room> getRoom();
这与RoomOption.hpp中的这段代码有关,它是Option的一个子类:
public:
shared_ptr<Room> getRoom();
以下是我遇到问题的两个类的所有代码:
Option.h:
#pragma once
#include "Room.h"
#include <memory>
using namespace std;
class Option
{
protected:
int id;
char* text;
public:
Option(void);
Option(int, char*);
virtual ~Option(void);
char* getText();
int getID();
virtual shared_ptr<Room> getRoom();
};
Option.cpp:
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
Option::Option(void)
{
}
Option::Option(int newID, char* newText){
id = newID;
text = newText;
}
Option::~Option(void)
{
}
char* Option::getText(){
return text;
}
int Option::getID(){
return id;
}
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room());
return room;
//note that this function will never be used. I'd prefer to
//pass back a null pointer but I couldn't do that either.
}
RoomOption.h:
#pragma once
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
class RoomOption :
public Option
{
private:
shared_ptr<Room> room;
public:
RoomOption(void);
RoomOption(int, char*, shared_ptr<Room>);
~RoomOption(void);
void setRoom(shared_ptr<Room>);
shared_ptr<Room> getRoom();
};
RoomOption.cpp:
#include "RoomOption.h"
#include "Room.h"
#include <memory>
using namespace std;
RoomOption::RoomOption(void)
{
}
RoomOption::RoomOption(int newID, char* newText, shared_ptr<Room> newRoom)
{
id = newID;
strcpy(text, newText);
room = newRoom;
}
RoomOption::~RoomOption(void)
{
}
void RoomOption::setRoom(shared_ptr<Room> newRoom){
room = newRoom;
}
shared_ptr<Room> RoomOption::getRoom(){
return room;
}
答案 0 :(得分:0)
此代码在/ W4 / WX与VS 2010编译时没有错误:
#include <memory>
struct Room {};
class Option {
public:
std::shared_ptr<Room> getRoom();
};
std::shared_ptr<Room> Option::getRoom(){
std::shared_ptr<Room> room(new Room());
return room;
}
int main() {
Option opt;
std::shared_ptr<Room> room = opt.getRoom();
return 0;
}
你有什么不同的做法?
答案 1 :(得分:-1)
Room
是否在Option.hpp的getRoom()
调用中使用了()
?
您是否尝试从new Room()
移除{{1}},以防您以某种方式被most vexing parse击中,可能在我们看不到的其他代码中?