G++;错误:在包含的文件中,未声明Foo 我的C++代码有问题。如果我在neuron.hpp中插入#include“god.hpp”,g++会显示以下错误: In file included from neuron.hpp:4, from main.cpp:5: god.hpp:11: error: ‘Neuron’ has not been declared god.hpp:13: error: ‘Neuron’ was not declared in this scope god.hpp:13: error: template argument 1 is invalid god.hpp:13: error: template argument 2 is invalid main.cpp: In function ‘int main()’: main.cpp:36: error: no matching function for call to ‘God::regNeuron(Neuron*&)’ god.hpp:11: note: candidates are: long int God::regNeuron(int*) In file included from god.hpp:5, from god.cpp:3: neuron.hpp:10: error: ‘God’ has not been declared In file included from neuron.hpp:4, from neuron.cpp:2: god.hpp:11: error: ‘Neuron’ has not been declared god.hpp:13: error: ‘Neuron’ was not declared in this scope god.hpp:13: error: template argument 1 is invalid god.hpp:13: error: template argument 2 is invalid

G++;错误:在包含的文件中,未声明Foo 我的C++代码有问题。如果我在neuron.hpp中插入#include“god.hpp”,g++会显示以下错误: In file included from neuron.hpp:4, from main.cpp:5: god.hpp:11: error: ‘Neuron’ has not been declared god.hpp:13: error: ‘Neuron’ was not declared in this scope god.hpp:13: error: template argument 1 is invalid god.hpp:13: error: template argument 2 is invalid main.cpp: In function ‘int main()’: main.cpp:36: error: no matching function for call to ‘God::regNeuron(Neuron*&)’ god.hpp:11: note: candidates are: long int God::regNeuron(int*) In file included from god.hpp:5, from god.cpp:3: neuron.hpp:10: error: ‘God’ has not been declared In file included from neuron.hpp:4, from neuron.cpp:2: god.hpp:11: error: ‘Neuron’ has not been declared god.hpp:13: error: ‘Neuron’ was not declared in this scope god.hpp:13: error: template argument 1 is invalid god.hpp:13: error: template argument 2 is invalid,c++,include,g++,declare,C++,Include,G++,Declare,以下是必要文件的相关部分: //main.cpp #include <string> #include <vector> #include "functions.hpp" #include "neuron.hpp" #include "god.hpp" using namespace std; int main() { God * god = new God(); vector<string>::iterator it; for(it = patterns

以下是必要文件的相关部分:

//main.cpp
#include <string>
#include <vector>
#include "functions.hpp"
#include "neuron.hpp"
#include "god.hpp"
using namespace std;

int main()
{
God * god = new God();

vector<string>::iterator it;
for(it = patterns.begin(); it != patterns.end(); ++it) {
    Neuron * n = new Neuron();
            god->regNeuron(n);
    delete n;
    cout << *it << "\n";
}
}
除去

并将其替换为转发声明:

//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1

class God;  //forward declaration

class Neuron
{
public:
    Neuron();
    void setGod(God *g);
};
#endif
同样适用于上帝.hpp:

//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1 

#include <vector>

class Neuron; //forward declaration

class God
{
public:
    God();
    long regNeuron(Neuron * n);
private:
    std::vector<Neuron*> neurons;
};
#endif
//god.hpp
#ifndef GOD_水电站
#定义水电站1
#包括
类神经元//远期申报
阶级神
{
公众:
上帝();
长regNeuron(Neuron*n);
私人:
载体神经元;
};
#恩迪夫
请注意,您需要在实现文件中包含。(
cpp
文件)


如果您使用对象的指针或引用作为成员,或者使用该类型作为返回类型或参数,则不需要完整定义,因此转发声明就足够了。

您具有循环依赖关系include。我还发现了一些问题:-尽量不使用带指针的标准容器,避免使用原始指针(您真的需要它们吗),你真的需要“新”吗?尝试对迭代器使用for(:)的新版本。
g++ -Wall -o getneurons main.cpp functions.cpp god.cpp neuron.cpp
#include "god.hpp" 
//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1

class God;  //forward declaration

class Neuron
{
public:
    Neuron();
    void setGod(God *g);
};
#endif
//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1 

#include <vector>

class Neuron; //forward declaration

class God
{
public:
    God();
    long regNeuron(Neuron * n);
private:
    std::vector<Neuron*> neurons;
};
#endif