C ++嵌套名称空间错误-预期的类型说明符错误

时间:2019-01-04 15:14:26

标签: c++ namespaces

我一直在寻找解决此问题的方法,但由于嵌套的命名空间,我认为它有所帮助。

下面是其中的相关部分:

implementation.hpp 是接口的实现

#ifndef IMPLEMENTATION_H
#define IMPLEMENTATION_H

#include "class_b.hpp"

namespace XPTO {
class Implementation : public XPTO::Interface {

    public:

        Implementation();
        ~Implementation() override;

        ReturnStatus
        initialize() override;

    private:

        CLASS_B::Class_B b; // namespace CLASS_B, class Class_B
    };
}
#endif

implementation.cpp

#include "implementation.hpp"

XPTO::Implementation::Implementation() {}

XPTO::ReturnStatus
XPTO::Implementation::initialize() {
    b = new CLASS_B::Class_B::Class_B(); 
    //namespace ClASS_B, class Class_B and constructor Class_B()
}

class_b.hpp

#ifndef CLASS_B_H
#define CLASS_B_H

namespace CLASS_B{

class Class_B {

    public:

        Class_B();
        ~Class_B();

        void initialize();
    };
}
#endif

错误是 错误:预期的类型说明符b =新的CLASS_B :: Class_B :: Class_B();

编译器指向命名空间CLASS_B。

2 个答案:

答案 0 :(得分:3)

我认为这是您的问题:

scala> val df = Seq(("A","105182","10"), ("A","105182","10" ), ("A","114256","11"), ("A","127855","12"), ("A","125182","12"), ("B","136234","13"), ("B","133468","13")).toDF("emp","subdept","dept")
df: org.apache.spark.sql.DataFrame = [emp: string, subdept: string ... 1 more field]

scala> df.printSchema
root
 |-- emp: string (nullable = true)
 |-- subdept: string (nullable = true)
 |-- dept: string (nullable = true)


scala> df.show
+---+-------+----+
|emp|subdept|dept|
+---+-------+----+
|  A| 105182|  10|
|  A| 105182|  10|
|  A| 114256|  11|
|  A| 127855|  12|
|  A| 125182|  12|
|  B| 136234|  13|
|  B| 133468|  13|
+---+-------+----+


scala> val df2 = df.withColumn("dept2",substring('subdept,3,7))
df2: org.apache.spark.sql.DataFrame = [emp: string, subdept: string ... 2 more fields]

scala> df2.createOrReplaceTempView("salaman")

scala> spark.sql(""" select *, size(collect_set(subdept) over(partition by emp)) sub_dep_count, size(collect_set(dept) over(partition by emp)) dep_count from salaman """).show(false)
+---+-------+----+-----+-------------+---------+
|emp|subdept|dept|dept2|sub_dep_count|dep_count|
+---+-------+----+-----+-------------+---------+
|B  |136234 |13  |6234 |2            |1        |
|B  |133468 |13  |3468 |2            |1        |
|A  |105182 |10  |5182 |4            |3        |
|A  |105182 |10  |5182 |4            |3        |
|A  |125182 |12  |5182 |4            |3        |
|A  |114256 |11  |4256 |4            |3        |
|A  |127855 |12  |7855 |4            |3        |
+---+-------+----+-----+-------------+---------+


scala>

它只需要是:

b = new CLASS_B::Class_B::Class_B(); 

假设b已经在其他地方声明了:

b = new CLASS_B::Class_B(); 

如果要使用CLASS_B::Class_B()* b; 分配一个内存块,则需要一个指向该内存块的指针。

答案 1 :(得分:3)

您在声明中缺少b的类型,该类型必须在标识符名称之前指定。尝试更改

b = new CLASS_B::Class_B::Class_B(); 

CLASS_B::Class_B *b = new CLASS_B::Class_B(); 

如果打算在initialize()方法中初始化私有成员b,则需要将其声明为指针,因为new返回了指针。