震撼:invalid use of undefined type
using namespace std;
class String
{
struct Srep; //representation.
Srep* rep;
public:
class CRef; //reference to char
class Range{}; //for exceptions
String(); // x=""
String( const char* ); //x="abc"
String( const String& ); //x=other_string
String& operator=(const char* );
String& operator=(const String&);
~String();
void check( int i ) const
{
if( i < 0 || rep->sz <= i ) // <-这一句编译报错: invalid use of undefined type `struct String::Srep'
{
throw Range();
}
}
};
struct String::Srep
{
char* s; //pointer to elements
int sz; //number of characters
int n; //reference count;
Srep( int nsz, const char* p )
{
n = 1;
sz = nsz;
s = new char[sz+1]; //add space for teminitor
strcpy(s,p);
}
~Srep() { delete[] s; }
Srep* get_own_copy() //clone if necessary
{
if( n== 1 ) return this;
n--;
return new Srep( sz , s );
}
void assign( int nsz , const char* p )
{
if( sz != nsz )
{
delete[] s;
sz = nsz;
s = new char[sz+1];
}
strcpy( s, p );
}
private: //prevent copying
Srep( const Srep& );
Srep operator=(const Srep&);
};
编译出错的部分在这里
void check( int i ) const
{
if( i < 0 || rep->sz <= i ) // <-这一句编译报错: invalid use of undefined type `struct String::Srep'
{
throw Range();
}
}
根据错误提示,因为我把struct String::Srep的定义放在了下面,所以这里找不到struct String::Srep的定义,
可是如果我把struct String::Srep的定义放在String类定义的上面,又会提示找不到String类,到底该怎么办那?
请高手指教,谢谢
相关推荐
Leetcode——144,二叉树前序遍历
/** * \* Created: liuhuichao * \* Date: 2019/8/21 * \* Time: 27:39 PM * \* Description: 二叉树的前序遍历 : 前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树 * \ * 给定一个二叉树,返回它的 前序 遍历。 * <p> * 示例: * &l...
leetcode 144(二叉树的前序遍历)
水题一道,题目如下 给定一个二叉树,返回它的 前序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 题解 1. 递归 import java.util.ArrayList; /** * Definition for a binary tree ...
【LeetCode 中等 树 python3】144. 二叉树的前序遍历
想要看更加舒服的排版、更加准时的推送 关注公众号“不太灵光的程序员” 每日八点有干货推送,微信随时解答你的疑问 144. 二叉树的前序遍历 python3 中等 树 迭代算法 给定一个二叉树,返回它的 前序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 前序遍历 根-左-右 from typing import List # Definition for a .
idea2025-free.zip
idea2025-free.zip
机械三维3D设计图-高炉开铁口钻钎机构系统设计.zip
机械三维3D设计图-高炉开铁口钻钎机构系统设计.zip


