不完整类型 - >无法从我的对象访问指针值

时间:2013-12-06 22:02:11

标签: c++ pointers inheritance

我正在尝试访问我班级中的一个指针,但是我似乎无法让VS确认指针在那里。

private:
   ForwardTo* forward;

以下是我从班级中抓取它的方法

ForwardTo& Persons::getForwardTo() const
{
    return *forward;
}

ForwardTo类型是一个继承的类,通常读取:

    class ForwardToTwo : public ForwardTo
{

public:
    ForwardToTwo(unsigned int strategy);
    virtual std::vector<std::string> forwardMessage(Persons& person, Message& message);
    unsigned int getStrategy() const { return strategy;};
private:
    unsigned int strategy;
};

最后,这是我试图访问指针的方式

listOfPersons.at(i).getForwardTo()->forwardMessage(listOfPersons.at(i), tempMessage);

这种访问方式以前用于我的类中的OTHER指针,其作用与此类指针完全相同。

在VS中键入行以访问指针时,intelliSense会选择最多getForwardTo()的所有函数,之后,点运算符/箭头运算符不会显示对函数的任何访问权限。< / p>

再次感谢您的帮助。

(编辑,我知道函数fordwardMessage()函数将返回一个向量,我只是在VS中输入,直到intelliSense无法检测到可访问的函数)

(编辑2,我尝试了.运算符和->运算符,但都不允许intelliSense检测任何函数。)

(编辑3,附加代码: 这是我的人员类标题:

#ifndef PERSONS_HPP
#define PERSONS_HPP

#include <string>
#include <vector>
#include <list>
#include <map>

#include "Message.hpp"

#include "TypeOne.hpp"
#include "TypeTwo.hpp"
#include "TypeThree.hpp"
#include "TypeFour.hpp"
#include "TypeFive.hpp"

class ForwardTo;
class ForwardToOne;
class ForwardToTwo;
class ForwardToThree;

class Persons
{
public:

    Persons();
    ~Persons();

    void setEmailAddress(std::string email);
    std::string getEmailAddress() const;
    const std::vector<std::string>& getContactList() const;

    void addMessageSeen(Message message);
    void addMessageContent(MessageContent mc);
    void addInboxMessage(Message message);
    void addContact(std::string contact);

    void viewContact(const std::vector<std::string>& contacts);
    void bumpContact();

    void setMessageTypeOne();
    void setMessageTypeTwo(unsigned int type);
    void setMessageTypeThree(unsigned int quality);
    void setMessageTypeFour(unsigned int type, unsigned int quality);
    void setMessageTypeFive();

    void setForwardTypeOne(unsigned int strategy);
    void setForwardTypeTwo(unsigned int strategy);
    void setForwardTypeThree(unsigned int strategy);

    void printPersonsObj();
    std::list<Message> getInbox() const;

    MessageType& getForwardWhen() const;
    ForwardTo& getForwardTo() const;

private:

    std::map<MessageContent, unsigned int>  messageList;
    std::list<Message> inbox;
    std::vector<std::string> contactList;

    std::string emailAddress;
    ForwardTo* forward;
    MessageType* forwardWhen;
};

这是我的Persons.cpp文件:

#include "Persons.hpp"

#include "ForwardToOne.hpp"
#include "ForwardToTwo.hpp"
#include "ForwardToThree.hpp"

#include <iostream>

Persons::Persons()
    :emailAddress(""), contactList(), inbox(), messageList()
{
}

Persons::~Persons()
{
    //delete forwardWhen;
    //delete forwardTo;
}

void Persons::addMessageContent(MessageContent mc)
{
    //messageSeen.insert(mc);
}

void Persons::setEmailAddress(std::string email)
{
    emailAddress = email;
}

std::string Persons::getEmailAddress() const
{
    return emailAddress;
}

void Persons::addContact(std::string contact)
{
    contactList.push_back(contact);
}

void Persons::addInboxMessage(Message message)
{
    inbox.push_back(message);
}

void Persons::viewContact(const std::vector<std::string>& contacts)
{
    for(auto i = contacts.begin(); i != contacts.end(); i ++)
    {
        std::cout << *i << std::endl;;
    }
}

void Persons::setMessageTypeOne()
{
    MessageType* forwardWhen = new TypeOne();
}

void Persons::setMessageTypeTwo(unsigned int type)
{
    MessageType* forwardWhen = new TypeTwo(type);
}

void Persons::setMessageTypeThree(unsigned int quality)
{
    MessageType* forwardWhen = new TypeThree(quality);
}

void Persons::setMessageTypeFour(unsigned int type, unsigned int quality)
{
    MessageType* forwardWhen = new TypeFour(type, quality);
}

void Persons::setMessageTypeFive() 
{
    MessageType* forwardWhen = new TypeFive();
}

void Persons::setForwardTypeOne(unsigned int strategy)
{
    ForwardTo* forward = new ForwardToOne(strategy);
}

void Persons::setForwardTypeTwo(unsigned int strategy)
{
    ForwardTo* forward = new ForwardToTwo(strategy);
}

void Persons::setForwardTypeThree(unsigned int strategy)
{
    ForwardTo* forward = new ForwardToThree(strategy);
}

const std::vector<std::string>& Persons::getContactList() const
{
    return contactList;
}

void Persons::bumpContact()
{
    std::vector<std::string> tempList = getContactList();
    std::string tempContact = tempList.at(0);
    for(unsigned int i = 0; i <= tempList.size(); i ++)
    {
        if(i == tempList.size())
            tempList.at(--i) = tempContact;
        else
            tempList.at(--i) = tempList.at(i);
    }
}

void Persons::addMessageSeen(Message message)
{
    messageList[*message.getMessageContent()] = message.getMessageContent()->getUniqueID();
}

void Persons::printPersonsObj()
{
    std::cout << "PERSONS OBJECT!" << std::endl;
    std::cout << "E-mail Address: " << emailAddress << std::endl;
    std::cout << std::endl;
}

std::list<Message> Persons::getInbox() const
{
    return inbox;
}

MessageType& Persons::getForwardWhen() const
{
    return *forwardWhen;
}

ForwardTo& Persons::getForwardTo() const
{
    return *forward;
}

2 个答案:

答案 0 :(得分:2)

getForwardTo()返回T&,只需使用.访问forwardMessage()

答案 1 :(得分:2)

不完整类型通常意味着在您尝试使用getForwardTo时,您还没有完全声明ForwardTo类。事实上,在你的person.hop中只有一个前瞻声明。

确保在包含呼叫站点的文件中包含完全声明ForwardTo的标头。