我在cpp。
的头文件中使用extern变量时遇到了一些问题所以我的(简化的)cppfiles如下:
#ifndef HEADER_H
#define HEADER_H
#include <string>
#include <list>
struct BstNode;
class Point;
extern std::list<Point> coordinates;
BstNode* Insert(BstNode* root, int data, std::string position);
void IterateLeft(BstNode* root);
void IterateRight(BstNode* root);
bool SearchAll(BstNode* root,int data);
int CalculateHeight(BstNode* root);
int CalculateWidth(BstNode* root);
#endif
#include <iostream>
#include "Header.h"
using namespace std;
BstNode* TestFunction();
int main() {
BstNode* testTree = TestFunction();
cout<<"\nWidth: "<<CalculateWidth(testTree)<<"\n";
cout<<"\nHeight: "<<CalculateHeight(testTree)<<"\n";
system("pause");
return 0;
}
BstNode* TestFunction() {
cout<<"Creating test-structure...";
BstNode* testTree = NULL;
testTree = Insert(testTree,17,""); //cout<<"17";
testTree = Insert(testTree,16,"0"); //cout<<"16";
testTree = Insert(testTree,15,"00"); //cout<<"15";
testTree = Insert(testTree,14,"000"); //cout<<"14";
testTree = Insert(testTree,13,"0000"); //cout<<"13";
testTree = Insert(testTree,12,"00000"); //cout<<"12";
testTree = Insert(testTree,8, "001"); //cout<<"8";
testTree = Insert(testTree,7, "0010"); //cout<<"7";
testTree = Insert(testTree,21,"0011"); //cout<<"21";
testTree = Insert(testTree,20,"00110"); //cout<<"20";
return testTree;
}
#include<iostream>
#include<string>
#include<algorithm>
#include<list>
#include<math.h>
#include "Header.h"
using namespace std;
//Node Definition
struct BstNode {
int data;
BstNode* left;
BstNode* right;
};
BstNode* GetNewNode(int data) {
BstNode* newNode = new BstNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
BstNode* testTree = NULL;
// Class to represent points.
class Point {
private:
int xval, yval, val;
public:
Point(int x=0, int y=0, int v=0) {
xval = x;
yval = y;
val = v;
}
int x() { return xval; }
int y() { return yval; }
int v() { return val; }
friend bool operator== (const Point a,const Point b) {
return (a.xval==b.xval && a.yval==b.yval);
}
};
// Calculates width
int CalculateWidth(BstNode* root){
int width;
if (root->left==NULL && root->right==NULL) {/*cout<<"leaf\n";*/ width = 1;}
else {
if (root->left==NULL) {width = CalculateWidth(root->right); /*cout<<"left=NULL\n";*/}
if (root->right==NULL) {width = 1 + CalculateWidth(root->left); /*cout<<"right=NULL\n";*/}
}
if (root->left!=NULL && root->right!=NULL) width = max(1+CalculateWidth(root->left),CalculateWidth(root->right));
return width;
}
// Calculates height of svg
int x=0,y=0;
list<Point> coordinates;
void IterateLeft(BstNode* root);
void IterateRight(BstNode* root);
int CalculateHeight(BstNode* root) {
int height=0;
int highest=0;
int lowest=0;
cout<<"check"; // /////////THIS WILL BE DISPLAYED
coordinates.push_front(Point(x,y,root->data));
cout<<"check"; // /////////THIS WONT ANYMORE
if(root->left!=NULL) IterateLeft(root->left);
if(root->right!=NULL) IterateRight(root->right);
for (list<Point>::iterator i = coordinates.begin(); i!=coordinates.end(); i++) {
if((*i).y()>highest) highest = (*i).y();
if((*i).y()<lowest ) lowest = (*i).y();
cout<<"("<< (*i).x() << "," << (*i).y() << ")\n";}
height = highest - lowest + 1;
return height;
}
// Helpfunction for CalculateHeight
void IterateLeft(BstNode* root) {
x++;
if(find(coordinates.begin(),coordinates.end(),Point(x,y))!=coordinates.end()) {IterateRight(root);}
else {
coordinates.push_front(Point(x,y,root->data));
if(root->left!=NULL) IterateLeft(root->left);
if(root->right!=NULL) IterateRight(root->right);
}
x--;
}
// Helpfunction for CalculateHeight
void IterateRight(BstNode* root) {
if(y>0) {y=y*(-1);}
else {y=y*(-1)+1;}
bool occupied = false;
for(int bench = CalculateWidth(root); bench>=0; bench--) {
if(find(coordinates.begin(),coordinates.end(),Point(x+bench,y))!=coordinates.end()) occupied=true;
}
if(occupied) {IterateRight(root);}
else {
coordinates.push_front(Point(x-1,y,root->data));
coordinates.push_front(Point(x,y,root->data));
if(root->left!=NULL) IterateLeft(root->left);
if(root->right!=NULL) IterateRight(root->right);
}
if(y>0) {y=(y-1)*(-1);}
else {y=y*(-1);}
}
// Insert a new node at a certain Position: 0 means left - 1 means right
BstNode* Insert(BstNode* root, int data, string position){
if (root == NULL) {
if (position.empty()) {
root = GetNewNode(data);
}
else {
root = GetNewNode(0);
Insert(root,data,position);
}
}
else {
if (position.empty()) {
root = GetNewNode(data);
}
else {
if (position[0] == '0') {
root->left = Insert(root->left,data,position.erase(0,1));
}
else {
root->right = Insert(root->right,data,position.erase(0,1));
}
}
}
return root;
}
//Looks for an element in Tree everywhere -> true if found
bool SearchAll(BstNode* root,int data) {
if(root == NULL) {
return false;
}
else if(root->data == data) {
return true;
}
else {
return (SearchAll(root->left,data) | SearchAll(root->right,data));
}
}
现在当我编译它时,它将编译。但是在运行该程序时,它会中止告诉我
CXX0030:错误:无法评估表达式
任何人都有任何暗示问题可能是什么?
错误出现在CalculateHeight定义中的Casual.cpp中。该行说:
coordinates.push_front(Point(x,y,root->data));
答案 0 :(得分:0)
添加
的定义后BstNode* GetNewNode( int d ){
BstNode* res = new BstNode();
res->data = d;
return res;
}
程序执行:
Creating test-structure...
Width: 6
checkcheck(3,-1)
(2,-1)
(1,-1)
(3,1)
(2,1)
(1,1)
(5,0)
(4,0)
(3,0)
(2,0)
(1,0)
(0,0)
Height: 3
我没有验证这个; - )