简体   繁体   English

在另一个类声明中创建一个类的对象时出现“预期类型说明符”错误

[英]“Expected a type specifier” error when creating an object of a class inside another class declaration

I have a class called scratch and have used scratch.h to declare it. 我有一个名为scratch的类,并已使用scratch.h对其进行了声明。

Now I have another class called scratch2 under scratch2.h and want to create an object of scratch as a shared pointer . 现在我有下scratch2.h另一个类称为scratch2,想从头开始创建一个对象作为共享指针

This is the syntax I used inside scratch2 class declartion: 这是我在scratch2类声明中使用的语法:

std::shared_ptr<scratch> newObject(new scratch());

But I am getting this error: Error: Expected type specifier 但我收到此错误: Error: Expected type specifier

So I tried this instead: 所以我尝试这样做:

std::shared_ptr<scratch> newObject2 = std::make_shared<scratch>();

which works fine. 效果很好。 Can anyone please tell me why the first one isn't working? 谁能告诉我为什么第一个不工作?

My scratch.h code: 我的scratch.h代码:

#ifndef _SCRATCH_
#define _SCRATCH_

#include <iostream>

class scratch {
private:
    int _a;
    float _b;
    std::string _s;
public:
    scratch();
    scratch(int a, float b, std::string n);
    ~scratch();
};
#endif

and my scratch2.h: 和我的scratch2.h:

#ifndef _SCRATCH_2_
#define _SCRATCH_2_

#include "scratch.h"
#include <memory>

class scratch2 {
    std::shared_ptr<scratch> newObject(new scratch()); // Expected a type specifier error occurs here
    std::shared_ptr<scratch> newObject2 = std::make_shared<scratch>(); // works fine here
};

#endif

Because in the context of declaring class members: 因为在声明类成员的上下文中:

std::shared_ptr<scratch> newObject(new scratch());

This initially looks to the compiler as a class method declaration. 最初,它将编译器视为类方法声明。 C++'s syntax is very complicated. C ++的语法非常复杂。 You can look at the entire declaration and understand what it's trying to do, but the compiler is parsing keywords one keyword at a time, and sees this: 您可以查看整个声明并了解它要执行的操作,但是编译器一次解析一个关键字,并且看到以下内容:

type name ( ... 类型 名称 (...

inside a class declaration, and this starts to look like a class method declaration, and that's what the compiler tried to parse, and failed. 在类声明中,它开始看起来像类方法声明,这就是编译器试图解析的结果,但失败了。

The formal specification of the C++ language spills a lot of ink on the subject of how things should be declared, mindful of the current state of compiler technology. 考虑到编译器技术的当前状态,C ++语言的正式规范在应该如何声明事物的主题上花了很多墨水。

You need to work with the compiler, and use an alternate syntax that's unambiguous: 您需要使用编译器,并使用明确的替代语法:

std::shared_ptr<scratch> newObject = std::shared_ptr<scratch>(new scratch());

Verified with gcc 5.3 已通过gcc 5.3验证

Inside of a class definition, there are only two ways you're allowed to initialize your members. 在类定义内部,只有两种方式可以用来初始化成员。 You can use = and you can use {} . 您可以使用= ,也可以使用{} You are not allowed to use () : 您不可以使用()

struct foo {
    int x = 4;  // OK
    int y{7};   // OK
    int z(12);  // error
};

Admittedly, the compiler error in this case is extremely unhelpful. 诚然,这种情况下的编译器错误是毫无帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM