【问题标题】:Expected class-name before { token - very simple{ 令牌之前的预期类名 - 非常简单
【发布时间】:2015-05-30 16:04:42
【问题描述】:

我已经看到了很多关于这个问题的答案,知道要寻找什么并且仍然可以看到。看起来有些明显的问题。

算法.h:

#ifndef ALGORITHM_H
#define ALGORITHM_H

#include <iostream>
using namespace std;

template <typename T>

class Algorithm
{
private:
    T data;
    T result;
public:
    Algorithm(T in){
        data = in;
    }

    void compute();

    void displayData(){
        cout<<data<<endl;
    }

    T getResult(){
        return result;
    }
};

#endif // ALGORITHM_H

泡泡.h:

#ifndef BUBBLE_H
#define BUBBLE_H

#include "algorithm.h"

class Bubble : public Algorithm{
public:
    Bubble();
};

#endif // BUBBLE_H

main.cpp

#include "bubble.h"
#include <iostream>
using namespace std;

int main()
{
    Algorithm<int> a(1);
    Algorithm<char> b('a');

    a.displayData();
    b.displayData();


    return 0;
}

错误是:

/home/user/Projects/Algorithms/main.cpp:1:在包含的文件中 ../Algorithms/main.cpp:1:0: /home/user/Projects/Algorithms/bubble.h:6: 错误:“{”令牌类气泡之前的预期类名:公共 算法{ ^

为什么编译器看不到算法类?我将它包含在 Bubble.h 中,所以呢?

【问题讨论】:

    标签: c++ inheritance


    【解决方案1】:

    您忘记为Algorithm 提供模板参数。如果你解决了这个问题,你的代码编译得很好。 (Live)

    【讨论】:

      【解决方案2】:

      Bubble 继承自 Algorithm 类,它是一个模板。所以还需要模板规范:

      #ifndef BUBBLE_H
      #define BUBBLE_H
      
      #include "algorithm.h"
      
      template <typename T>
      class Bubble : public Algorithm<T> {
      public:
          Bubble();
      };
      
      #endif // BUBBLE_H
      

      【讨论】:

      • 虽然这篇文章可能会回答这个问题,但添加一些解释并可能添加一些指向相关文档的链接仍然是一个好主意。具有良好解释和参考的答案通常对当前的 OP 和未来的访问者都更有用。完整详细的答案也更有可能吸引正面投票。
      • 是的 - 你是对的。正如您可能从我的其他回复中注意到的那样,我通常会这样做 :) 我在这里遇到了紧急情况,并且已经输入了密码...
      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      相关资源
      最近更新 更多