各位大哥大姐帮忙看看错误 为什么就是不能得出结果呢?

suixinpiaodang 2011-05-26 12:22:58
头文件
quad.h

#ifndef _QUADRANGLE_H_
#define _QUADRANGLE_H_
#include<iostream>
#include<string>
using namespace std;

class Quadrangle
{
public:
string name; //图形标志

};
typedef Quadrangle *QUADPTR;

#endif


para.h
#ifndef _PARA_H_//平行四边形
#define _PARA_H_

using namespace std;
#include<string>
//#include"quad.h"
class Parallelogram
{
public:
string name;//形体的标识
Parallelogram (int l , int h);
~Parallelogram();
string what();
double area();
void draw ();

private:
int length,height;//长和高
};

#endif
list.h

#ifndef _LIST_H_
#define _LIST_H_
#include<iostream>
using namespace std;
#include<string>
#include"quad.h"
typedef void (*ACCESSFUN)(QUADPTR); //定义遍历时节点处理函数类型
struct Node
{
QUADPTR data;
struct Node *next;
} ;
class List
{
private:
Node *head, *tail;

public:
List();
List(const List& l);
~List();
void push_back(const QUADPTR quad); //尾部添加
void traverse(ACCESSFUN f); //遍历
};

#endif
源文件
quad.cpp

#ifndef _QUADRANGLE_H_//
#define _QUADRANGLE_H_
#include<iostream>
#include<string>
using namespace std;

class Quadrangle
{
public:
string name; //

};
typedef Quadrangle *QUADPTR;

#endif

list.cpp
#include <iostream>//遍历和存储
#include <string>
using namespace std;



#include"quad.h"
#include "list.h"

List::List() : head(NULL), tail(NULL) {}

List::List(const List& l) : head(l.head ), tail(l.tail ) {}

List::~List()
{
Node *p = head, *q;
while (p != NULL)
{
q = p;
p = p->next;
delete q;
}
}//insert element into the tail of list
void List::push_back(const QUADPTR quad)
{ Node* newa;
newa = new Node;/*if(head == tail){ newa->data = quad;head = newa;newa->next = NULL;tail =head->next;*/
newa->data=quad;
newa->next=NULL;
if(tail==NULL){
head=newa;
tail=newa;
tail->next = NULL;
}
else
{ tail->next = newa;
tail=newa;
tail->next =NULL;
}//在链表尾部增加一个节点
}
void List::traverse(ACCESSFUN f)//遍历
{
Node *p = head;
while (p != NULL)
{
f(p->data);
p = p->next;
}
}
para.cpp

#include<iostream>//平行四边形
#include"para.h"
#include<string>
using namespace std;

Parallelogram:: Parallelogram(int l, int h) : name(" parallelogram"), length(l), height(h) {}
Parallelogram::~ Parallelogram() {}

string Parallelogram::what(){
return name;
}
void Parallelogram::draw(){
cout << what() <<": length = " <<length << ", height = " << height<<endl;
}
double Parallelogram::area(){
double s;
s =length*height;
return s;

}

main.cpp

#include <iostream>
#include <string>
#include "quad.h"
//#include "rect.h"
#include "list.h"
//#include "dioa.h"
#include "para.h"
//#include "trap.h"
//#include "squr.h"
using namespace std;
void access(QUADPTR quad)
{
/*if (quad->name == "rectangle")
{
Rectangle *p = reinterpret_cast <Rectangle *> (quad);
p->draw();
cout << "area = " << p->area() << endl;
}*/

/*if (quad->name == "square")
{
Square *p = reinterpret_cast<Square *>(quad);
p->draw();
cout << "area = " << p->area() << endl;
}*/
if (quad->name == "parallelogram")
{
Parallelogram *p = reinterpret_cast<Parallelogram *>(quad);
p->draw();
cout << "area = " << p->area() << endl;
}
/*if (quad->name == "trapezoid")
{
Trapezoid *p = reinterpret_cast<Trapezoid *>(quad);
p->draw();
cout << "area = " << p->area() << endl;
}*/
/* if(quad->name == "diamond")
{
Diamond *p = reinterpret_cast<Diamond *>(quad);
p->draw();
cout << "area = " << p->area() << endl;
}*/

}

int main()
{
// Rectangle rect (5,7);
//Square squr(5);
Parallelogram para(7,8);
//Trapezoid trap(2,4,6);
//Diamond dioa(4,5);//五种形体不同的对象
List list;
//list.push_back(reinterpret_cast<QUADPTR>(&rect));
//list.push_back(reinterpret_cast<QUADPTR>(&squr));
list.push_back(reinterpret_cast<QUADPTR>(¶));
//list.push_back(reinterpret_cast<QUADPTR>(&trap));
//list.push_back(reinterpret_cast<QUADPTR>(&dioa));
list.traverse(access);


运行后没有结果
想得到结果是 Parallelogram :length=7,height=8;
area=28
是怎么回事呢?


...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljt3969636 2011-05-26
  • 打赏
  • 举报
回复

para.cpp
看看这个构造函数:
Parallelogram:: Parallelogram(int l, int h) : name(" parallelogram"), length(l), height(h) {}
放大:
name(" parallelogram")中的" parallelogram"前面多打了个空格
导致:
access(QUADPTR quad)
{
if (quad->name == "parallelogram")//这句比较不成功...
{
suixinpiaodang 2011-05-26
  • 打赏
  • 举报
回复
我搞了好几天了
suixinpiaodang 2011-05-26
  • 打赏
  • 举报
回复
大哥你实在太厉害了。。。。。佩服佩服 谢谢哈。。。。。
Defonds 2011-05-26
  • 打赏
  • 举报
回复
空格都看出来了,比编译器还牛叉
maoxing63570 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hnuqinhuan 的回复:]
牛B 这都看出来了啊
[/Quote]
無_1024 2011-05-26
  • 打赏
  • 举报
回复
牛B 这都看出来了啊

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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