【问题标题】:C++ undefined symbolC++ 未定义符号
【发布时间】:2012-09-05 00:49:32
【问题描述】:

我正在创建一个 C++ 控制台应用程序,我在其中保存向量并将其加载到文件中。我正在保存和加载的文件有一个具有矢量大小的标题。

这是我的代码:

void loadFromFile()
    {
        ifstream iStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        iStream.read((char*)&fHeader, sizeof(fileHeader_t));
        if (fHeader.magicNumber = 0xDEADBEAF)
        {
            appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
        }
    }
    void saveToFile()
    {
        ofstream oStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        fHeader.magicNumber = 0xDEADBEAF;
        fHeader.appointmentCount = appointments.size();
        oStream.write((char*)&fHeader, sizeof(fileHeader_t));
        oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
    }

这是标题结构:

struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentsCount;
}fileHeader_t;

我收到以下错误:

E2379 语句丢失; E2451 未定义符号'fHeader'

在以下几行:

fileHeader_t fHeader;

为什么会发生这种情况,更重要的是,我该如何解决?

谢谢

更新

这是我的完整代码:

class appointment
{
public:
    appointment(string aDate, string aTime, string aType,
    string aLocation, string aComments, bool aIsImportant,
    string aReminderDate, string aReminderTime)
    {
        appDate = aDate;
        appTime = aTime;
        appType = aType;
        appLocation = aLocation;
        appComments = aComments;
        appIsImportant = aIsImportant;
        appReminderDate = aReminderDate;
        appReminderTime = aReminderTime;
    }
    void setDate(string aDate)
    {
        appDate = aDate;
    }
    void setTime(string aTime)
    {
        appTime = aTime;
    }
    void setType(string aType)
    {
        appType = aType;
    }
    void setLocation(string aLocation)
    {
        appLocation = aLocation;
    }
    void setComments(string aComments)
    {
        appComments = aComments;
    }
    void setIsImportant(bool aIsImportant)
    {
        appIsImportant = aIsImportant;
    }
    void setReminderDate(string aReminderDate)
    {
        appReminderDate = aReminderDate;
    }
    void setReminderTime(string aReminderTime)
    {
        appReminderTime = aReminderTime;
    }
    string getDate()
    {
        return appDate;
    }
    string getTime()
    {
        return appTime;
    }
    string getType()
    {
        return appType;
    }
    string getLocation()
    {
        return appLocation;
    }
    string getComments()
    {
        return appComments;
    }
    bool getIsImportant()
    {
        return appIsImportant;
    }
    string getReminderDate()
    {
        return appReminderDate;
    }
    string getReminderTime()
    {
        return appReminderTime;
    }
private:
    appointment();
    string appDate;
    string appTime;
    string appType;
    string appLocation;
    string appComments;
    bool appIsImportant;
    string appReminderDate;
    string appReminderTime;
    //person owner;
};

class calendar
{
public:
    calendar()
    {
        loadFromFile();
    }
    ~calendar()
    {
        saveToFile();
    }

    void createAppointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime)
    {
        appointment newAppointment(aDate, aTime, aType, aLocation, aComments, aIsImportant, aReminderDate, aReminderTime);
        appointments.push_back(newAppointment);
    }

private:
    vector<appointment> appointments;
    string calCurrentDate;
    string calCurrentTime;
    void loadFromFile()
    {
        ifstream iStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        iStream.read((char*)&fHeader, sizeof(fileHeader_t));
        if (fHeader.magicNumber = 0xDEADBEAF)
        {
            appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
        }
    }
    void saveToFile()
    {
        ofstream oStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        fHeader.magicNumber = 0xDEADBEAF;
        fHeader.appointmentCount = appointments.size();
        oStream.write((char*)&fHeader, sizeof(fileHeader_t));
        oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
    }
    typedef struct fileHeader_s
    {
        DWORD magicNumber;
        size_t appointmentCount;
    }fileHeader_t;
};

我收到以下 2 个错误:

[BCC32 警告] Person.cpp(271): W8060 可能分配不正确 完整的解析器上下文 Person.cpp(245):班级日历 Person.cpp(290):决定实例化:void calendar::loadFromFile() --- 为实例化重置解析器上下文... Person.cpp(267): 解析: void calendar::loadFromFile()

[BCC32 错误] 向量 (608): E2247 'appointment::appointment()' 不可访问 完整的解析器上下文 矢量(607):决定实例化:无效矢量>::调整大小(无符号整数) --- 为实例化重置解析器上下文... Person.cpp(18): #include c:\program files (x86)\embarcadero\rad studio\9.0\include\dinkumware\vector 矢量(8):命名空间标准 矢量(330):类矢量<_ty> vector(607): 解析: void vector >::resize(unsigned int)

我可以帮忙解决这个问题吗?

【问题讨论】:

  • 你忘记了struct
  • 谢谢。我已经更新了我的问题。我可以帮忙吗?
  • @DarrylJanecek:提示:问题是private: appointment();
  • 在预约课上?我想在那里将其设为私有,以便在创建对象时必须输入值。在使用向量和保存/加载到文件时我不应该这样做吗?
  • 好的,我已将值更改为 public... 现在我收到以下错误:[ILINK32 错误] 错误:未解决的外部 'appointment::appointment()' 引用自 H:\2012 \TRIMESTER 2\IT6253 - C++ PROGRAMMING\ASSESSMENT 2\WIN32\DEBUG\PERSON.OBJ 和 [ILINK32 错误] 错误:无法执行链接

标签: c++ file-io istream ostream


【解决方案1】:

您在结构定义中缺少 typedef 关键字。

typedef struct fileHeader_s
{

}fileHeader_t;

但是,在 C++ 中,typedef 关键字不是必需的。在这种情况下,结构名称仍然是fileHeader_s

【讨论】:

    【解决方案2】:

    我想你想要的是typedef

    typedef struct fileHeader_s
    {
    DWORD magicNumber;
    size_t appointmentsCount;
    }fileHeader_t;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2011-11-19
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多