我无法理解为什么我的编译器会给我这些错误:
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我无法理解为什么它不认为“FACT”是一种数据类型
答案 0 :(得分:4)
您无法声明指向引用的指针,就像您尝试执行此操作一样:
FACT(const FACT& *copy)
没有指向引用的指针。可能你只想要一个没有指针的引用:
FACT(const FACT& copy)
答案 1 :(得分:2)
您正在尝试声明复制构造函数。但是这不正确。
根据标准
中的以下引用声明复制构造函数$ 12.8 / 2 - “非模板构造函数 对于类X,是一个复制构造函数if 它的第一个参数是X&amp;类型, const X&amp;,volatile X&amp;或const 易变的X&amp;和,或者没有 其他参数或其他所有参数 参数有默认参数 (8.3.6)。“
'应该是复制构造函数'的参数具有类型'指向引用的指针'。根据下面的引用,标准不允许这样做
$ 8.3.2 / 5-“应该没有 对引用的引用,没有数组 引用,没有指向 引用“。
答案 2 :(得分:1)
嗯,你的第一个错误就在这里:
FACT(const FACT& *copy)
不能这样做,你想要一个参考
FACT(const FACT& copy)
答案 3 :(得分:1)
当然@noah无法宣布
指向参考的指针
但允许反转,即对指针的引用。
FACT(const FACT *©)
是一种有效的语法,在某些情况下非常有用。虽然,它仍然是一个错误的复制构造函数。
有很多好的网页在教这个:
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