未声明奇怪的“类名”

时间:2020-04-16 12:12:53

标签: c++ c++11

container.hpp

#ifndef CONTAINER_HPP
#define CONTAINER_HPP

#include <functional>


namespace lasd {

/* ************************************************************************** */

class Container {

private:

  // ...

protected:

  unsigned long size = 0;

public:

  // Destructor
  virtual ~Container() = default;

  /* ************************************************************************ */

  // Copy assignment
  Container& operator=(const Container&) = delete; // Not usable.

  // Move assignment
  Container& operator=(Container&) = delete; // Not usable.

  /* ************************************************************************ */

  // Comparison operators
  bool operator==(const Container&) const noexcept = delete; // Not usable.
  bool operator!=(const Container&) const noexcept = delete; // Not usable.

  /* ************************************************************************ */

  // Specific member functions

  virtual inline bool Empty() const noexcept;

  virtual inline unsigned long Size() const noexcept;

  virtual void Clear() = 0;

};

#include "container.cpp"

}

#endif

container.cpp

// Specific member functions (Container)


inline unsigned long Container::Size() const noexcept{
  return size;
}

inline bool Container::Empty() const noexcept{
  return size == 0;
}

将此作为输出。最有趣的部分是所有这些代码都是由我的教授提供的,而我只编码了.cpp文件。我已经尝试将#include“ container.hpp”添加到我的.cpp文件中。

||=== Build: Debug in Exercise1 (compiler: GNU GCC Compiler) ===|
||=== Build: Debug in Exercise1 (compiler: GNU GCC Compiler) ===|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|4|error: 'Container' has not been declared|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|4|error: non-member function 'long unsigned int Size()' cannot have cv-qualifier|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp||In function 'long unsigned int Size()':|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|5|error: 'size' was not declared in this scope|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|8|error: 'Container' has not been declared|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|8|error: non-member function 'bool Empty()' cannot have cv-qualifier|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp||In function 'bool Empty()':|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|9|error: 'size' was not declared in this scope|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|38|error: expected initializer before '<' token|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

在教授视频讲座期间,他已经编译了代码,并且效果很好。我真的不明白为什么通过使用Code :: Blocks会给我这个错误(我也在Atom编辑器上的gpp-compiler上尝试过,结果完全一样)。

谢谢。

2 个答案:

答案 0 :(得分:1)

很有可能,在您的项目中,您将像任何合理的项目一样单独编译container.cpp。但是,您的教授做了一些永远不会通过任何代码审查的操作-他在头文件中包含了一个cpp文件。

您需要将项目更改为不单独编译container.cpp(或者更好的是,摆脱该包含并修复container.cpp以使其可以编译)。

答案 1 :(得分:0)

注意头文件中的namespace lasd。您应该在cpp文件(lasd::Container::Size)中显式命名每个函数的命名空间,或者也应将其包装在namespace lasd { ... }块中。

相关问题