Joseph问题,各种error.顿感十分无力。求救。。。

假老练 2011-09-23 03:47:22
我看了半天实在不知道哪里错了,相当无力。。。

至于每个类中的具体方法的实现就不贴了,我估计问题不在那里面,我就把自己写的几个类定义和简化再简化的主函数贴上来

具体的实现就用花括号代替了

请各位前辈,高人,大牛不吝赐教。。。

在此先谢了


/*******************JOSEPH.CPP*********************/

#include<string>
using namespace std;

//异常
class NullLocationPointer {
private:
string info;
public:
NullLocationPointer():info("Location pointer is NULL.") {}
string& what() {return info;}
};

//采用单循环链表
//约定头结点为0号位置


//节点类
template<class T>
class Node {
friend class CircChain<T>;
friend class Iterator<T>;
private:
T data;
Node<T> *next;
public:
Node() {next=0;}
};

//单循环链表
template<class T>
class CircChain {
friend class Iterator<T>;
private:
Node<T> *head,*tail; //头结点尾结点
int size; //链表长度
public:
CircChain(const T *tArray,int n=10); //仅仅专门针对这个问题方便起见,传个数组来构造单循环链表
~CircChain();
int getSize() const {return size;} //获取表长
};

//迭代器
template<class T>
class Iterator {
private:
CircChain<T> &backup_target; //表示某个迭代器指向的是哪个链表对象
Node<T> *before_location;
Node<T> *location; //迭代器指向的结点
public:
Iterator(const CircChain<T> &x):backup_target(x),location(x.head) {
before_location=location;
}
void next() {} //向后挪
void toHead() {} //挪到头结点处
void refresh() {} //指向另一个链
T getData() const throw NullLocationPointer {} //获取数据
void del() {} //删除当前指向的结点
};

//主函数

int main() {
int a[]={1,2,3,4,5};
CircChain<int> c(a,5);
return 0;
}


编译错误:

c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(21): error C2059: 语法错误:“<”
1> c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(28): 参见对正在编译的类 模板 实例化“Node<T>”的引用
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(21): error C2238: 意外的标记位于“;”之前
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(22): error C2059: 语法错误:“<”
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(22): error C2238: 意外的标记位于“;”之前
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(41): error C2989: “CircChain”: 类 模板 已经声明为非类 模板
1> c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(21) : 参见“CircChain”的声明
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(31): error C3857: “CircChain”: 不允许使用多个 模板 参数列表
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(59): error C2989: “Iterator”: 类 模板 已经声明为非类 模板
1> c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(22) : 参见“Iterator”的声明
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(44): error C3857: “Iterator”: 不允许使用多个 模板 参数列表
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(63): error C2143: 语法错误 : 缺少“;”(在“<”的前面)
1>c:\users\ray\documents\visual studio 2010\projects\second\second\test2.cpp(63): error C2143: 语法错误 : 缺少“;”(在“<”的前面)
1>
1>生成失败。
1>
1>已用时间 00:00:01.53
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

无解。。。。

我写了个类似的来看哪里有错,却能编译过,不知道为什么


/***************纯属试一试.CPP****************/
template<class T>
class Iterator {
private:
T location;
};

template<class T>
class Chain {
friend class Iterator<T>;
private:
T head;
};

template<class T>
class Node {
friend class Iterator<T>;
friend class Chain<T>;
private:
T next;
};

int main() {
Chain<int> c;
return 0;
}


编译过了,只有个变量c未被引用的warning

/********************************************************分割线**********************************************/

怎么回事啊??
...全文
116 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
打爆你眼镜 2011-09-23
  • 打赏
  • 举报
回复
mark
君恪 2011-09-23
  • 打赏
  • 举报
回复
赵老师这方法给力啊~我还傻傻的用链表在做
bellbird 2011-09-23
  • 打赏
  • 举报
回复

/*******************JOSEPH.CPP*********************/

#include<string>
using namespace std;

//异常
class NullLocationPointer {
private:
string info;
public:
NullLocationPointer():info("Location pointer is NULL.") {}
string& what() {return info;}
};

//采用单循环链表
//约定头结点为0号位置


//节点类
template<class T>
class Node {
friend class CircChain; //这里不需要<T>
friend class Iterator; //同样
private:
T data;
Node<T> *next;
public:
Node() {next=0;}
};

//单循环链表
template<class T>
class CircChain {
// friend class Iterator<T>; 多余
private:
Node<T> *head,*tail; //头结点尾结点
int size; //链表长度
public:
CircChain(const T *tArray,int n=10); //仅仅专门针对这个问题方便起见,传个数组来构造单循环链表
~CircChain();
int getSize() const {return size;} //获取表长
};

//迭代器
template<class T>
class Iterator {
private:
CircChain<T> &backup_target; //表示某个迭代器指向的是哪个链表对象
Node<T> *before_location;
Node<T> *location; //迭代器指向的结点
public:
Iterator(const CircChain<T> &x):backup_target(x),location(x.head) {
before_location=location;
}
void next() {} //向后挪
void toHead() {} //挪到头结点处
void refresh() {} //指向另一个链
T getData() const throw NullLocationPointer {} //获取数据
void del() {} //删除当前指向的结点
};

//主函数

int main() {
int a[]={1,2,3,4,5};
CircChain<int> c(a,5);
return 0;
}

赵4老师 2011-09-23
  • 打赏
  • 举报
回复
记不得哪位C++大牛在哪本学习C++的书的前言里面说过
“用C语言1000行源码能完成的工作千万不要用C++重写!”
赵4老师 2011-09-23
  • 打赏
  • 举报
回复
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}
就想叫yoko 2011-09-23
  • 打赏
  • 举报
回复
帮顶。
君恪 2011-09-23
  • 打赏
  • 举报
回复
表示我只写过C语言的,代码在家里- -

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧