簡體   English   中英

使用Qt Creator,為什么我的類頭文件不能編譯?

[英]Using Qt creator, why can't my class header compile?

在此處輸入圖片說明

我寫了兩個類頭文件。 在包含兩個標題之前,該項目已成功構建。 但是,將它們包含在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'

以下是wy_StrBlobPtr.h的代碼,該error: expected ')' before '&' token引用error: expected ')' before '&' token

#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:include防護。

去掉

#include <wy_StrBlobPtr.h>

來自wy_StrBlob.h

class wy_StrBlobPtr需要的定義wy_StrBlob已知的,但是通過包括頭文件, wy_StrBlobPtr之前被定義wy_StrBlob符號是已知的編譯器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM