gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:10:56 25 4
gpt4 key购买 nike

我正在尝试访问我类(class)中的一个指针,但我似乎甚至无法让 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);

这种访问方式以前适用于我类(class)中的 OTHER 指针,它的行为与这个完全相同。

在 VS 中键入访问指针输出的行时,intelliSense 选取所有函数直到 getForwardTo(),之后,点运算符/箭头运算符不会显示任何访问函数。

再次感谢所有帮助。

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

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

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

#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;
}

最佳答案

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

确保您在包含调用站点的文件中包含了完全声明 ForwardTo 的 header 。

关于c++ - 不完整类型 --> 无法从我的对象访问指针值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20434368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com