无法声明指向“const class FOO”的指针错误

发布于 2024-10-02 10:55:31 字数 3037 浏览 3 评论 0原文

我不明白为什么我的编译器给我这些错误:

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”是一种数据类型

I can't understand why my compiler is giving me these errors:

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;
}

I'm using g++ 4.4.3 I can't understand why it doesn't consider "FACT" to be a data type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此岸叶落 2024-10-09 10:55:31

您不能声明指向引用的指针,就像您尝试在此处所做的那样:

FACT(const FACT& *copy)

不存在指向引用的指针之类的东西。也许您只是想要一个引用,而不需要指针:

FACT(const FACT& copy)

You can't declare a pointer to a reference, like you try to do here:

FACT(const FACT& *copy)

There is no such thing as pointers to a reference. Probably you just wanted a reference, without a pointer:

FACT(const FACT& copy)
染年凉城似染瑾 2024-10-09 10:55:31

您正在尝试声明一个复制构造函数。然而这是不正确的。

复制构造函数是根据以下标准引用声明的

$12.8/2 - “非模板构造函数
对于类 X 是一个复制构造函数,如果
它的第一个参数是 X& 类型,
const X&、易失性 X&或常量
易失性 X&,并且要么没有
其他参数或其他所有
参数有默认参数
(8.3.6)。”

“应该是复制构造函数”的参数具有“指向引用的指针”类型。根据下面的引用,标准不允许这样做

$8.3.2/5-“不得有
引用到引用,没有数组
引用,并且没有指向
参考文献。”

You are attempting to declare a copy constructor. However it is not correct.

A copy constructor is declared as per the below quote from the Standard

$12.8/2 - "A non-template constructor
for class X is a copy constructor if
its first parameter is of type X&,
const X&, volatile X& or const
volatile X&, and either there are no
other parameters or else all other
parameters have default arguments
(8.3.6)."

The parameter of the 'supposed to be copy constructor' has type 'pointer to reference'. This is disallowed by the Standard as per the quote below

$8.3.2/5- "There shall be no
references to references, no arrays of
references, and no pointers to
references."

紙鸢 2024-10-09 10:55:31

好吧,你的第一个错误在这里:

FACT(const FACT& *copy)

不能这样做,你需要一个参考

FACT(const FACT& copy)

Well, your first error is here:

FACT(const FACT& *copy)

Can't do that, you want a reference

FACT(const FACT& copy)
谁与争疯 2024-10-09 10:55:31

嘿,人们。这并不全是事实。

当然@noah不能声明

指向引用的指针

但也允许相反的情况,即对指针的引用。

FACT(const FACT *©)

是一种有效的语法,在某些情况下非常有用。尽管如此,它仍然是一个错误的复制构造函数。

有几个很好的页面介绍了这一点:

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

Hey, people. It is not all the truth.

Of course @noah can't declare

a pointer to a reference

but the inverse, a reference to a pointer, is allowed.

FACT(const FACT *©)

is a valid syntax and very useful in some situations. Although, it is still a wrong copy constructor.

There are couples of good pages teaching this:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文