使用Qt创建器,为什么我的类头不能编译?

 薛薛Sying 发布于 2023-02-08 11:46

在此输入图像描述

我写了两个类头文件.在包含两个标题之前,项目已成功构建.但是在将它们包含在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.

#ifndef WY_STRBLOBPTR_H
#define WY_STRBLOBPTR_H

#include 
#include 
#include 
#include 
#include 

class wy_StrBlobPtr
{
public:
    typedef std::vector tp;

    wy_StrBlobPtr() : curr(0) {}

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

private:
    std::weak_ptr wp;
    std::size_t curr;
};

#endif // WY_STRBLOBPTR_H

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

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

#ifndef WY_STRBLOB_H
#define WY_STRBLOB_H
#include 
#include 
#include 
#include 

class wy_StrBlobPtr;

class wy_StrBlob
{
    friend class wy_StrBlobPtr;

public:
    typedef std::vector::size_type size_type;

    wy_StrBlob() :
        data(std::make_shared>()) {}

    wy_StrBlob(std::initializer_list   il) :
        data(std::make_shared>(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> 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:包括警卫.

1 个回答
  • 去掉

    #include <wy_StrBlobPtr.h>
    

    wy_StrBlob.h

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

    2023-02-08 11:48 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有