1

enter image description here

I wrote two class header files. Before the two headers were included, the project was built successfully. But after they were included in the main.cpp , as shown in the image attached, it was complained while building that

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'

Below is the code of wy_StrBlobPtr.h to which error: expected ')' before '&' token was refering.

#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

What's the problem here?How to fix it?

Update:codes of wy_StrBlob.h. Definitions for the members are omitted for simplicity. Please let me know , if they are needed.

#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 guards are added.

11
  • 2
    Could you show the declaration of wy_StrBlob please (contents of wy_StrBlob.h respectively)? Commented Dec 25, 2013 at 0:29
  • 2
    You have a cyclic dependency. Remove #include <wy_StrBlobPtr.h> from wy_StrBlob.h. Commented Dec 25, 2013 at 0:45
  • 1
    I think removing #include <wy_StrBlobPtr.h> should do the job. Didn't spot this immediately ... Commented Dec 25, 2013 at 0:56
  • 1
    @Alan.W Additionally do not inline any code referring to wy_StrBlobPtr, but move these to an extra compilation unit. Commented Dec 25, 2013 at 1:02
  • 1
    @Alan.W As a rule of thumb yes! It will deviate when you're going to introduce template classes. But as long you're using simple class declarations/definitions follow that rule. You might want to inline stuff like getter/setter functions that do not depend to wy_StrBlobPtr in the header though. Commented Dec 25, 2013 at 2:35

1 Answer 1

6

Remove

#include <wy_StrBlobPtr.h>

from wy_StrBlob.h

class wy_StrBlobPtr needs the definition of wy_StrBlob known, however by including the header file, wy_StrBlobPtr is being defined before wy_StrBlob symbol is known to the compiler.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried but didn't work..still complained the same thing error: expected ')' before '&' token.
I just tried your code without the include line, it compiles just fine.
Yes,you are right!! I had misunderstood your post and removed the forward declaration class wy_StrBlobPtr; I'm so stupid..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.