【问题标题】:Using Qt creator, why can't my class header compile?使用 Qt creator,为什么我的类头不能编译?
【发布时间】:2013-12-25 00:27:07
【问题描述】:

我写了两个类头文件。在包含两个头文件之前,项目已成功构建。但是在将它们包含在 main.cpp 中后,如附图所示,在构建时抱怨

12:54:13: Starting: "/usr/bin/make" 
g++ -c -pipe -g -std=c++0x -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -I/usr/share/qt5/mkspecs/linux-g++ -I../CPP_Primer_ch2 -I. -o main.o ../CPP_Primer_ch2/main.cpp
In file included from ../CPP_Primer_ch2/wy_StrBlob.h:19:0,
             from ../CPP_Primer_ch2/main.cpp:9:
../CPP_Primer_ch2/wy_StrBlobPtr.h:30:30: error: expected ')' before '&' token
make: *** [main.o] Error 1
12:54:15: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project CPP_Primer_ch2 (kit: Desktop)
When executing step 'Make'

下面是error: expected ')' before '&' token 所引用的wy_StrBlobPtr.h 的代码。

#ifndef WY_STRBLOBPTR_H
#define WY_STRBLOBPTR_H

#include <string>
#include <vector>
#include <memory>
#include <wy_StrBlob.h>
#include <stdexcept>

class wy_StrBlobPtr
{
public:
    typedef std::vector<std::string> tp;

    wy_StrBlobPtr() : curr(0) {}

    wy_StrBlobPtr(wy_StrBlob &sb, std::size_t sz = 0) : wp(sb.data), curr(sz) {}
  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private:
    std::weak_ptr<tp> wp;
    std::size_t curr;
};

#endif // WY_STRBLOBPTR_H

这里有什么问题?如何解决?

更新:wy_StrBlob.h 的代码。为简单起见,省略了成员的定义。如果需要,请告诉我。

#ifndef WY_STRBLOB_H
#define WY_STRBLOB_H
#include <string>
#include <vector>
#include <memory>
#include <wy_StrBlobPtr.h>

class wy_StrBlobPtr;

class wy_StrBlob
{
    friend class wy_StrBlobPtr;

public:
    typedef std::vector<std::string>::size_type size_type;

    wy_StrBlob() :
        data(std::make_shared<std::vector<std::string>>()) {}

    wy_StrBlob(std::initializer_list<std::string>   il) :
        data(std::make_shared<std::vector<std::string>>(il)) {}

    size_type size() const { return data->size(); }
    bool empty() const { return data->empty(); }

    //! add and remove
    void push_back(const std::string &s) { data->push_back(s);}
    void pop_back();

    //! elements access
    std::string& front();
    const std::string& front() const ;

    std::string& back();
    const std::string& back() const ;

    wy_StrBlobPtr begin();  //return wy_StrBlobPtr to the first element
    wy_StrBlobPtr end();    //return one past the last element

private:
    std::shared_ptr<std::vector<std::string>> data;
    //! throws msg if data[i] isn't valid
    void check(size_type i, const std::string &msg) const;
};
#endif // WY_STRBLOB_H

Upadte 2nd:添加了包括警卫。

【问题讨论】:

  • 能否请您出示wy_StrBlob的声明(分别为wy_StrBlob.h的内容)?
  • 你有一个循环依赖。从 wy_StrBlob.h 中删除 #include
  • 我认为删除 #include &lt;wy_StrBlobPtr.h&gt; 应该可以完成这项工作。没有立即发现...
  • @Alan.W 另外,不要内联任何引用 wy_StrBlobPtr 的代码,而是将它们移至额外的编译单元。
  • @Alan.W 根据经验,是的!当您要引入模板类时,它会有所偏差。但只要您使用简单的类声明/定义,就遵循该规则。不过,您可能希望在标题中内联不依赖于 wy_StrBlobPtr 的 getter/setter 函数。

标签: c++ qt qt-creator smart-pointers


【解决方案1】:

删除

#include <wy_StrBlobPtr.h>

来自wy_StrBlob.h

class wy_StrBlobPtr 需要知道wy_StrBlob 的定义,但是通过包含头文件,wy_StrBlobPtr 是在编译器知道wy_StrBlob 符号之前定义的。

【讨论】:

  • 我试过但没有用..仍然抱怨同样的事情error: expected ')' before '&amp;' token。
  • 我刚试过你的代码没有包含行,它编译得很好。
  • 是的,你是对的!!我误解了你的帖子并删除了前向声明类wy_StrBlobPtr;我太愚蠢了..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多