前向声明被忽略? [英] Forward declaration being ignored?

查看:31
本文介绍了前向声明被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类(基本上是 std::vector 的包装器),其中一个函数返回 AInteger 类型的变量(基本上是 int 的包装器).现在 AInteger 类型在整个类中多次使用,但编译器开始在一个非常特定的位置抱怨.当我删除getSize()"函数时,一切都编译得很好.

I have the below class (which is basically a wrapper for std::vector), and one of the functions returns a variable of the type AInteger (which is basically a wrapper for int). Now the type AInteger is used multiple times throughout the class, yet the compiler starts complaining at a very specific position. When I remove the "getSize()" function, everything compiles just fine.

相互包容,所以我需要预先声明才能让一切正常工作.

There is mutual inclusion, so I need the forward declaration for everything to work.

问题之一是 AList 类是一个模板,因此无法将定义移动到 .cpp 文件(通常可以解决问题).

One of the problems is that the AList class is a template, so it is impossible to move the definitions to a .cpp file (which would normally solve the problem).

我做错了什么?

这是课程:

#ifndef ALIST_H
#define ALIST_H

#include <vector>

#include "AInteger.h"

class AInteger;

template<typename VALUE>
class AList {
public:
    AList() {
    }

    AList(const std::vector<VALUE> list) {
        value = list;
    }

    ~AList() {
    }

    operator const std::vector<VALUE>() const {
        return value;
    }

    std::vector<VALUE> toStdVector() const {
        return value;
    }

    VALUE operator [](const AInteger index) const {
        return value.at(index);
    }

    void add(const VALUE value) {
        this->value.push_back(value);
    }

    VALUE get(const AInteger index) const {
        return value[index];
    }

    AInteger getSize() const { // ERROR OCCURS HERE
        return value.size();
    }

    void remove(const AInteger index) {
        value.erase(index);
    }

private:
    std::vector<VALUE> value;
};

#endif

输出:

1>------ Build started: Project: ALibrary, Configuration: Debug Win32 ------
1>  ASocket.cpp
1>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
1>  AInteger.cpp
1>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
1>  AHttpRequest.cpp
1>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
1>  ABoolean.cpp
1>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
1>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
1>  Generating Code...
1>  Compiling...
1>  AString.cpp
1>  Generating Code...
2>------ Build started: Project: BitHoarder, Configuration: Debug Win32 ------
2>  SystemHandler.cpp
2>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
2>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
2>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
2>  Main.cpp
2>d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(43): error C2027: use of undefined type 'AInteger'
2>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(8): note: see declaration of 'AInteger'
2>  d:\programs\programming\visual studio projects\c++\alibrary\alibrary\alist.h(53): note: see reference to class template instantiation 'AList<VALUE>' being compiled
2>  Generating Code...
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========

如果需要更多代码,请询问,我会提供.

If any more code is needed just ask and I will provide.

谢谢!

这是AInteger.h"中的代码

Here's the code that's inside "AInteger.h"

#ifndef AINTEGER_H
#define AINTEGER_H

#include "AList.h"

template<typename VALUE>
class AList;

class AInteger {
public:
    AInteger();
    AInteger(const int);
    ~AInteger();

    operator const int() const;

    int toInt() const;

    AInteger operator=(const AInteger &);
    AInteger & operator++();
    AInteger & operator--();

    AList<AInteger>splitByNumber(const AInteger &);

private:
    int value;
};

#endif

更令人困惑的是,以下执行 THE EXACT SAME THING 的类不会产生错误:

To make matters even more confusing, the following class, which does THE EXACT SAME THING does not produce the error:

#ifndef ADICTIONARY_H
#define ADICTIONARY_H

#include <map>

#include "AInteger.h"

class AInteger;

template<typename KEY, typename VALUE, typename COMPARE = std::less<KEY>>
class ADictionary {
public:
    ADictionary() {
    }

    ADictionary(const std::map<KEY, VALUE, COMPARE> dictionary) {
        value = dictionary;
    }

    ~ADictionary() {
    }

    operator const std::map<KEY, VALUE, COMPARE>() const {
        return value;
    }

    std::map<KEY, VALUE, COMPARE> toStdMap() const {
        return value;
    }

    VALUE operator [](const KEY key) const {
        return value.at(key);
    }

    void add(const KEY key, const VALUE value) {
        this->value.insert(std::make_pair(key, value));
    }

    VALUE get(const KEY key) const {
        return value[key];
    }

    AInteger getSize() const { // No error here
        return value.size();
    }

    void remove(const KEY key) {
        value.erase(value.find(key));
    }

private:
    std::map<KEY, VALUE, COMPARE> value;
};

#endif

推荐答案

getSize() 的实现必须创建一个 AInteger 的实例.这只有在 AInteger 的完整定义已知时才有可能.您不能创建仅向前声明的类型的实例.

The implementation of getSize() must create an instance of AInteger. This is only possible if the full definition of AInteger is known. You cannot create an instance of a type which is only forward-declared.

这正是编译器告诉你的:error C2027: use of undefined type 'AInteger'.它不会忽略前置声明,而是告诉您这还不够.

And that's exactly what the compiler tells you: error C2027: use of undefined type 'AInteger'. It does not ignore the forward declaration but tells you that it's not enough.

至于#include "AInteger.h",你不显示它的内容,但可能的问题是:

As for the #include "AInteger.h", you do not show its contents, but possible problems are:

  • 头文件中的错误包含保护导致编译器跳过定义.
  • 头文件定义了一个具有相似名称的不同类型.
  • AInteger 的定义在命名空间内.
  • Buggy include guard in the header file causes the compiler to skip the definition.
  • The header file defines a different type with a similar name.
  • The definition of AInteger is within a namespace.

这篇关于前向声明被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆