2010-11-16 60 views
2

我不明白爲什麼我的編譯器是給我的這些錯誤:不能聲明指針「常量類Foo&」錯誤

brain.cpp:16: error: cannot declare pointer to ‘const class FACT&’ 
brain.cpp: In constructor ‘FACT::FACT(const FACT*)’: 
brain.cpp:20: error: cannot convert ‘FACT**’ to ‘FACT*’ in assignment 
brain.cpp: In member function ‘void FACT::AddRelation(FACT*)’: 
brain.cpp:29: error: expected type-specifier before ‘*’ token 
brain.cpp:29: error: cannot convert ‘int**’ to ‘FACT*’ in initialization 
brain.cpp:29: error: expected ‘,’ or ‘;’ before ‘FACT’ 
brain.cpp:35: error: expected type-specifier before ‘*’ token 
brain.cpp:35: error: cannot convert ‘int**’ to ‘FACT*’ in assignment 
brain.cpp:35: error: expected ‘;’ before ‘FACT’ 
brain.cpp: At global scope: 
brain.cpp:47: error: expected unqualified-id before ‘=’ token 
brain.cpp:48: error: expected type-specifier before ‘*’ token 
brain.cpp:48: error: cannot convert ‘int**’ to ‘FACT*’ in initialization 
brain.cpp:48: error: expected ‘,’ or ‘;’ before ‘FACT’ 
brain.cpp: In function ‘void AddFact(FACT*)’: 
brain.cpp:52: error: cannot convert ‘FACT**’ to ‘FACT*’ in initialization 
brain.cpp:58: error: expected type-specifier before ‘*’ token 
brain.cpp:58: error: cannot convert ‘int**’ to ‘FACT*’ in assignment 
brain.cpp:58: error: expected ‘;’ before ‘FACT’` 

#include <iostream> 
using namespace std; 
class FACT 
{ 
    public: 
     FACT(string f) 
     { 
      fact=f; 
      relations=NULL; 
      num_relations=0; 
     }; 
     ~FACT() 
     { 
      delete[] relations; 
     }; 
     FACT(const FACT& *copy) 
     { 
      num_relations=copy->num_relations; 
      delete[] relations; 
      relations=new FACT*[num_relations]; 
      for (int x=0; x<=num_relations; x++) 
      { 
       relations[x]=copy->relations[x]; 
      } 
      fact=copy->fact; 
     }; 
     void AddRelation(FACT *fact) 
     { 
      FACT *copy=new *FACT[num_relations]; 
      for (int x=0; x<=num_relations; x++) 
      { 
       copy[x]=relations[x]; 
      } 
      delete[] relations; 
      relations=new *FACT[num_relations+1]; 
      for (int x=0; x<=num_relations; x++) 
      { 
       relations[x]=copy[x]; 
      } 
      relations[num_relations+1]=fact; 
      num_relations++; 
     }; 
     string fact; 
     FACT *relations; 
     int num_relations; 
}; 
FACT *facts=new *FACT[0]; 
int num_facts=0; 
void AddFact(FACT *new_item) 
{ 
    FACT *copy=new FACT*[num_facts]; 
    for (int x=0; x<=num_facts; x++) 
    { 
     copy[x]=facts[x]; 
    } 
    delete[] facts; 
    facts=new *FACT[num_facts+1]; 
    for (int x=0; x<=num_facts; x++) 
    { 
     facts[x]=copy[x]; 
    } 
    delete[] copy; 
    num_facts++; 
    facts[num_facts]=new_item; 
} 
int main() 
{ 
    FACT *new_item=new FACT("linux is secure"); 
    AddFact(new_item); 
    delete[] facts; 
    return 0; 
} 

我用G ++ 4.4.3我可以「不懂爲什麼它不考慮‘事實’是一種數據類型

回答

4

你不能一個指針聲明爲參考,如你試圖在這裏做的:

FACT(const FACT& *copy) 

沒有指向引用的指針。也許你只是想一個參考,沒有指針:

FACT(const FACT& copy) 
+0

我認爲這是如何你做了拷貝構造函數 – noah 2010-11-16 02:09:25

+0

@noah:它沒有'*'。 – sth 2010-11-16 02:15:38

1

好了,你的第一個錯誤是在這裏:

FACT(const FACT& *copy) 

不能做到這一點,你想有一個參考

FACT(const FACT& copy) 
2

你正試圖聲明一個拷貝構造函數。然而,這是不正確的。

拷貝構造函數聲明爲每下面的報價標準

$ 12.8/2 - 「爲X類的非模板的構造 是,如果 它的第一個參數是類型的X拷貝構造函數&, 常量X &,揮發性X &或const 揮發性X &,並且或者沒有 其他參數,否則所有其他 參數都有默認參數 (8.3.6)。」

'應該是複製構造函數'的參數具有類型'指向引用的指針'。這是由標準按報價低於不允許

$ 8.3.2/5「應無 引用參考文獻,沒有 引用數組,並沒有指針指向 引用。」

+0

+1,比我的回答更完整 – 2010-11-16 07:39:39

1

嘿,人。這不是全部的事實。

當然@noah不能申報

一個指向參考

但倒數,以指針的引用,是允許的。

FACT(const FACT *&copy) 

是一種有效的語法,在某些情況下非常有用。儘管如此,它仍然是一個錯誤的拷貝構造函數。

有良好的頁面夫婦教這樣的:

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr390.htm http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html http://cplus.about.com/od/learning1/ss/constructors.htm