[英]C++ redefinition of function
我有一个基类 class 和 3 个单独的派生类。 所有.hpp 文件的结构都相同。
The.hpp 不起作用:
#ifndef CAESARCIPHER_HPP
#define CAESARCIPHER_HPP
#include "Cipher.hpp"
#include<string>
class CaesarCipher : public Cipher {
public:
CaesarCipher(Key key)
: Cipher{ key } {}
std::string getCipherTypeString() const override {}
};
#endif
它的.cpp(还没有实现任何东西):
#include "CaesarCipher.hpp"
#include "Cipher.hpp"
#include<iostream>
CaesarCipher::CaesarCipher(Key key)
: Cipher{ key } {}
std::string CaesarCipher::getCipherTypeString() const {
return "";
}
One.hpp 确实有效:
#ifndef ASCIICIPHER_HPP
#define ASCIICIPHER_HPP
#include "Cipher.hpp"
#include<string>
class AsciiCipher : public Cipher {
public:
AsciiCipher(Key key)
: Cipher{ key } {}
std::string getCipherTypeString() const override {}
};
#endif
base.hpp class 看起来像这样:
typedef uint64_t Key;
#ifndef BASE_HPP
#define BASE_HPP
#include <string>
class Cipher {
Key key_;
public:
Cipher(Key key){}
virtual std::string getCipherTypeString() const {}
};
#endif
错误:
CaesarCipher.cpp:6:15: error: redefinition of 'CaesarCipher'
CaesarCipher::CaesarCipher(Key key)
^
./CaesarCipher.hpp:9:9: note: previous definition is here
CaesarCipher(Key key)
^
CaesarCipher.cpp:9:27: error: redefinition of 'getCipherTypeString'
std::string CaesarCipher::getCipherTypeString() const {
^
./CaesarCipher.hpp:12:21: note: previous definition is here
std::string getCipherTypeString() const override {}
^
问题是派生类中有 2 个工作得很好,但是对于一个派生类,我得到了上面提到的所有函数的错误。 他们看起来都一样,只是改了名字等等。
你必须:
Key
。Base::Base(Key)
的定义提供缺少的主体。那应该可以解决您遇到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.