C++ - 不完整的类声明(incomplete class declarations ) 问题 和 解答

不完整的类声明(incomplete class declarations ) 问题 和 解答

 

两个类放入一个文件时, 如果包含内联函数, 及友元(friend)类, 则函数放置的顺序尤为重要, 类的前置声明(class forward declaration)也需要注意.

否则会产生编译错误: 不完整的类声明(incomplete class declarations)

错误: "error: return type 'class StrBlobPtr' is incomplete"

错误: "error: invalid use of incomplete type 'class StrBlobPtr'"

则需要修改类的成员函数的放置位置, 一般来说, 正确的写法为:

class B;  class A {  friend class B;  ......  };  class B {  ......  };  ... A::fcn {  ......  }  ... B::fcn {  ......  }  

声明在前, 实现在后, 如果是内联(inline)函数使用B类, 则有可能找不到B类的成员;

则会出现"不完整的类声明(incomplete class declarations);

如以下示例(C++ Primer, Eclipse CDT (GCC 4.8.1));

/*   * CppPrimer.cpp   *   *  Created on: 2013.11.6   *      Author: Caroline   */    /*eclipse cdt*/    #include <iostream>    #include <vector>  #include <string>  #include <initializer_list>    #include <memory>    #include <cstddef>  #include <stdexcept>    using namespace std;    class StrBlobPtr;    class StrBlob {  	friend class StrBlobPtr;  	public:  		typedef std::vector<std::string>::size_type size_type;  		StrBlob();  		StrBlob(std::initializer_list<std::string> il);  		size_type size() const{ return data->size(); }  		bool empty() const { return data->empty(); }  		void push_back(const std::string& t) { data->push_back(t); }  		void pop_back();  		std::string& front() ;  		std::string& back() ;  		StrBlobPtr begin();  		StrBlobPtr end();  	private:  		std::shared_ptr<std::vector<std::string> > data;  		void check(size_type i, const std::string &msg) const;  };    class StrBlobPtr {  	public:  		StrBlobPtr() : curr(0) {}  		StrBlobPtr(StrBlob &a, std::size_t sz = 0) : wptr(a.data), curr(sz) {}  		std::string& deref() const;  		StrBlobPtr& incr();  	private:  		std::shared_ptr<std::vector<std::string> >  		check(std::size_t, const std::string&) const;  		std::weak_ptr<std::vector<std::string> > wptr;  		std::size_t curr;  };    StrBlob::StrBlob():data(std::make_shared<std::vector<std::string>>()) {}  StrBlob::StrBlob(std::initializer_list<std::string> il):  		data(std::make_shared< std::vector<std::string> >(il)) {}  void StrBlob::check(size_type i, const std::string &msg) const {  	if(i >= data->size())  		throw out_of_range(msg);  }  std::string& StrBlob::front () {  	check(0, "front on empty StrBlob");  	return data->front();  }  std::string& StrBlob::back () {  	check(0, "back on empty StrBlob");  	return data->back();  }  void StrBlob::pop_back () {  	check(0, "pop_back on empty StrBlob");  	data->pop_back();  }  StrBlobPtr StrBlob::begin() {  	return StrBlobPtr(*this);  }  StrBlobPtr StrBlob::end() {  	auto ret = StrBlobPtr(*this, data->size());  	return ret;  }    std::shared_ptr<std::vector<std::string> >  StrBlobPtr::check(std::size_t i, const std::string& msg) const {  	auto ret = wptr.lock();  	if (!ret)  		throw std::runtime_error("unbound StrBlobPtr");  	if (i >= ret->size())  		throw std::out_of_range(msg);  	return ret;  }  std::string& StrBlobPtr::deref() const {  	auto p = check(curr, "dereference past end");  	return (*p)[curr];  }  StrBlobPtr& StrBlobPtr::incr() {  	check(curr, "increment past end of StrBlobPtr");  	++curr;  	return *this;  }    int main (void) {    	StrBlob b1;  	//{}释放b2  	{  		StrBlob b2 = {"baby", "girl", "lady", "woman", "female"};  		b1 = b2;  		b2.push_back("pretty");  	}  	std::cout << "b1 size = " << b1.size() << std::endl;  	//b2已经被释放了  	std::cout << "b1.begin() = " << b1.begin().deref() << std::endl;  	std::cout << "b1.begin().incr().deref() = "  			<< b1.begin().incr().deref() << std::endl;    	return 0;    }  

你可能感兴趣的:(C++,Class,forward,pointer,SMART,declaration,Declarations,Mystra)