简体   繁体   English

C++ function的重新定义

[英]C++ redefinition of function

I have a base class and 3 separate derived classes.我有一个基类 class 和 3 个单独的派生类。 All of the.hpp files are structured the same.所有.hpp 文件的结构都相同。

The.hpp which doesn't work: 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

The.cpp of it (haven't implemented anything yet):它的.cpp(还没有实现任何东西):

  #include "CaesarCipher.hpp"
    #include "Cipher.hpp"
    #include<iostream>
    
    CaesarCipher::CaesarCipher(Key key)
        : Cipher{ key } {}
    
    std::string CaesarCipher::getCipherTypeString() const {
        return "";
    }

One.hpp that does work: 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

The base.hpp class looks like this: 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

Errors:错误:

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 {}
                    ^

The problem is that 2 of the derived classes work perfectly but for one I get the above mentioned error for all of the functions.问题是派生类中有 2 个工作得很好,但是对于一个派生类,我得到了上面提到的所有函数的错误。 They look all the same just with changed names etc.他们看起来都一样,只是改了名字等等。

You have to:你必须:

  • Define the type Key .定义类型Key
  • Provide the missing body for the definition of Base::Base(Key) .Base::Base(Key)的定义提供缺少的主体。
  • Add a return statement to all functions that return non-void.向所有返回非 void 的函数添加 return 语句。

That should fix the errors that you are getting.那应该可以解决您遇到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM