cpp의 헤더 파일에 extern 변수를 사용할 때 몇 가지 문제가 있습니다.Cpp 헤더의 extern 변수 사용
header.h가
#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
MainFile.cpp
#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;
}
Casual.cpp
#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));
}
}
을 이제 다음과 같이
그래서 난 내 (간체) cppfiles이 내가 이것을 컴파일 할 때, 컴파일 될 것이다. 그러나 프로그램을 실행하는 동안 내게 말을 중단합니다
CXX0030: Error: expression cannot be evaluated
누구나 어떤 암시가있을 수 있습니까?
오류는 CalcualHeight의 정의에서 Casual.cpp에 나타납니다. 선은 말한다 :
coordinates.push_front(Point(x,y,root->data));
'통근 불구하고 '링커에게'coordinates'라는 변수가 있다는 것을 알려줍니다. 그래서 정확히 ** ** 정의 했습니까? casual.cpp의 – StoryTeller
에서 CalculateHeight 정의 전에 세 줄을 입력하십시오. – xamiax
이 오류는 디버거가 표현식을 평가하지 못했음을 나타냅니다. 프로그램이 왜 추락했는지와 관련이 없습니다. – Sneftel