2014-06-11 3 views
1

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)); 
+3

'통근 불구하고 '링커에게'coordinates'라는 변수가 있다는 것을 알려줍니다. 그래서 정확히 ** ** 정의 했습니까? casual.cpp의 – StoryTeller

+0

에서 CalculateHeight 정의 전에 세 줄을 입력하십시오. – xamiax

+1

이 오류는 디버거가 표현식을 평가하지 못했음을 나타냅니다. 프로그램이 왜 추락했는지와 관련이 없습니다. – Sneftel

답변

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 

내가 이것을 확인하지 않았다 ;-)

+0

오, 젠장! 나는 실수로 코드를 GetNewNode 붙여 넣기를 잘못해서 삭제했다. – xamiax

+0

이상한데, PC에서 실행됩니다 .../ – xamiax

+0

-Wall 및 -Wextra의 결과가 없습니다. - 디버거없이 실행 해보고 더 많은 출력을 추가하여 실패한 부분을 확인하고 실패한 부분에서 복잡한 표현을 분해하십시오. – laune