【问题标题】:<list> of pointers to an object | Template argument is invalid<list> 指向对象的指针 |模板参数无效
【发布时间】:2016-04-25 13:22:27
【问题描述】:

我是 C++ 和一般编程的新手。我必须在“NAVI”类中为我的课程编写一个程序,该程序将指向“Ort”类型的对象的指针(trans:Location)存储在列表中。 (“Navi”类还有更多功能,但请忽略它们)。它必须是一个列表,即使在我的情况下向量会更有意义。

编译时遇到的错误:

In file included from Ort.h:10:0,
                 from Ort.cpp:8:
NAVI.h:40:10: error: 'Ort' was not declared in this scope
list<Ort> *n;  
      ^
NAVI.h:40:13: error: template argument 1 is invalid
 list<Ort> *n;  
         ^
NAVI.h:40:13: error: template argument 2 is invalid

我的“Navi.h”标题:

#ifndef NAVI_H
#define NAVI_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <list>
#include "Ort.h"
#include <vector>
#include <sstream>

using namespace std;

class NAVI {
public:
    NAVI();
    NAVI(const NAVI& orig);
    virtual ~NAVI();

    virtual void RouteBerechnen();

    void namenAnVektor(string n, int x);
    void namenAusVektorLoeschen(string st);
    void namenInDatei();
    void NamenEinlesen();
    void AlleAusgeben();
    void alleDatenBinaerSpeichern();
    void alleDatenBinaerLaden();
    void nachOrtSuchen(string start, string ziel);

private:


    list<Ort> *n;  
    const std::string binaerPfad = "C:\\Users\\Karsten\\Desktop\\Skripte u.     Unterlagen\\2. Semester\\PAD 2\\Praktikum\\binaerdaten.bin";
    const std::string dateiPfad = "C:\\Users\\Karsten\\Desktop\\Skripte u. Unterlagen\\2. Semester\\PAD 2\\Praktikum\\schreiben.txt";
};

#endif  /* NAVI_H */

我的“Ort.h”:

#ifndef ORT_H
#define ORT_H
#include "NAVI.h"

class Ort {
public:
    Ort(int cords,std::string n);
    Ort();
    Ort(const Ort& orig);
    virtual ~Ort();

    std::string getName();
    int getCords();
    void setCords(int cords);
    void setName(std::string n);

private:
    int GPSKoordinaten;
    std::string Ortsname;
};

#endif  /* ORT_H */

非常感谢任何帮助:)

【问题讨论】:

  • 为什么Ort.h 包含Navi.h
  • 你说得对,没有理由在 Navi.h 中包含 Ort.h - 不知道我为什么这样做。这也解决了我的错误,但为什么呢? :) 和 tyvm!

标签: c++ list templates std


【解决方案1】:

#include 几乎就像编译器开始处理文件之前的复制/粘贴操作,这意味着即使 NAVI.h 被列在 Ort.h#include 中,它的内容也不会在使用 Ort 时已包含在内。

如果您在程序中包含Ort.h,那么编译器将看到以下内容:

#ifndef ORT_H
#define ORT_H
#include "NAVI.h" // ok, start with NAVI.h even though the compiler hasn't considered any of the rest of Ort.h yet

然后变成(与此答案无关的一些项目已删除):

#ifndef ORT_H
#define ORT_H
//#include "NAVI.h" // actual content of NAVI.h follows
#ifndef NAVI_H
#define NAVI_H
#include "Ort.h" // this is ignored, we already have #define ORT_H
#include <vector>
#include <sstream>

using namespace std;

class NAVI {
public:
    NAVI();
    ...
private:
    list<Ort> *n;  // Compiler hasn't seen Ort yet

因此,如果您将包含复制/粘贴到“看看编译器看到什么”,您就可以理解为什么 Ort 是未知的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2014-02-03
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多