2012-04-03 2 views
0

여기에 내가 그것을 컴파일 할 때 ambigious 기호 끝이 이유, 그것은 나에게이XOR 링크리스트의 구현

1>------ Build started: Project: xor_list, Configuration: Debug Win32 ------ 
1> xor_list.cpp 
1>c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(30): error C2872: 'end' : ambiguous symbol 
1>   could be 'c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(9) : node *end' 
1>   or  'end' 
1>c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(69): error C2872: 'end' : ambiguous symbol 
1>   could be 'c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(9) : node *end' 
1>   or  'end' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

같은 오류를 제공 XOR 링크리스트의 구현

#include<iostream> 

using namespace std; 

struct node 
{ 
    int v; 
    node *next; 
}; 

node *start=NULL; 
node *end=NULL; 

node *newnode(int v) 
{ 
    node *np=new node; 
    np->v=v; 
    np->next=NULL; 
    return np; 
} 

//returns the xor of two nodes 
node *xor(node *a,node *b) 
{ 
    return (node*)((long long)(a)^(long long)(b)); 
} 

void insert(node *current,node *prev,node *np) 
{ 
    if(start==NULL) 
    { 
     np->next=xor(prev,NULL); 
     start=np; 
     end=np; 
    } 
    else 
    { 
     insert(xor(prev,current->next),current,np); 
    } 
} 

void displayforward(node *start,node *prev) 
{ 
    if(start==NULL) return ; 
    cout<<start->v<<"-> "; 
    displayforward(xor(start->next,prev),start); 
} 

void displayBackward(node *end, node *prev){ 
    if(end == NULL) return; 

    cout<< end->v<<" -> "; 
    displayBackward(xor(end->next, prev), end); 
} 

int main() 
{ 
    int a[] = {1,2,3,4,5,6,7,8,9,10}, n = 10; 

    for(int i=0; i < n; i++){ 
     node *prev = NULL; 
     insert(start, prev, newnode(a[i])); 
    } 

    cout<<"Forward: \n"; 
    node *prev=NULL; 
    displayforward(start, prev); 

    cout<<"\nBackward: \n"; 
    displayBackward(end, prev); 

    return 0; 
} 

하지만 내 코드? 무엇을 이 오류는 의미합니까?

+0

'끝'노드 변수 이름을 'finish'와 같은 다른 이름으로 바꾸고 다시 컴파일하십시오. – Jaywalker

답변

5

컴파일러는 end 전역 변수를 사용할지 또는 std::end 심볼을 사용할지를 확신하지 못합니다. 이는 반복자를 컨테이너 끝에 반환하는 함수입니다. 그 상징에 어떤 것을 할당하는 것이별로 의미가 없다는 것은 다른 문제입니다. 이 특정 문제를 해결하려면 using namespace std; 지시문을 제거하십시오. 네임 스페이스 오염을 피하려면 using std::cout;으로 바꾸는 것이 좋습니다. 당신은 당신이 using namespace std;를 포함하지 않고 std::end로 끝을 선언해야

using namespace std;

를 사용하거나

1

프로젝트에 end이라는 다른 기호가있는 것 같습니다. 심볼의 이름을 변경해야합니다.

또는 end을 키워드로 정의한 확장 프로그램 또는 플러그인이있을 수 있습니다.

1

은 어느 당신은 모호성을 제거하려면 변수 이름을 변경해야합니다.

1

여기에 단일 XOR'ed 포인터를 사용하는 Doubly LL 구현입니다. 귀하의 의견을 던져주십시오. 감사!

#include <stdio.h> 
#include <stdlib.h> 

static struct node *nodeAlloc(); 
static struct node *xor(struct node *a, struct node *b); 
static void insertFirst(int); 
static void insertLast(int); 
void dumpList(struct node *, struct node *); 

struct node { 
    int value; 
    struct node *np; 
}; 

struct node *head; 
struct node *tail; 

static struct node *nodeAlloc() { 
    return (struct node *)malloc(sizeof(struct node)); 
} 

static struct node *xor(struct node *prev, struct node *next) { 
    return (struct node*)((unsigned long)prev^(unsigned long)next); 
} 

static void insertFirst(int value) { 
    struct node *x; 
    x = nodeAlloc(); 
    x->value = value; 

    if(head == NULL && tail == NULL) { 
     x->np = NULL; 
     head = x; 
     tail = x; 
    } else { 
     x->np = xor(NULL, head); 
     head->np = xor(x, head->np); 

     head = x;     
    } 
} 

static void insertLast(int value) { 
    struct node *x; 
    x = nodeAlloc(); 
    x->value = value; 

    if(head == NULL && tail == NULL) { 
     x->np = NULL; 
     head = x; 
     tail = x; 
    } else { 
     x->np = xor(tail, NULL); 
     tail->np = xor(tail->np, x); 

     tail = x; 
    } 
} 

void dumpList(struct node *node, struct node *prev) { 
    if(node != NULL) { 
     printf("\n%d", node->value); 
     dumpList(xor(node->np, prev), node); 
    } 
} 


//------------------ 

int main() { 
    insertFirst(7); 
    insertFirst(5); 
    insertFirst(4); 
    insertFirst(2); 
    insertLast(8); 
    insertLast(9); 
    insertLast(10); 


    printf("\n------------------\nForward List:"); 

    dumpList(head, NULL); 

    printf("\n------------------\nBackward List:"); 

    dumpList(tail, NULL); 

    return 0; 
}