簡體   English   中英

C++ function的重新定義

[英]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)的定義提供缺少的主體。
  • 向所有返回非 void 的函數添加 return 語句。

那應該可以解決您遇到的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM