Redefinition of class

前端 未结 3 874
再見小時候
再見小時候 2021-01-22 17:35

I got three .cpp files and two header files.

But when i compile them, meaning the Point.cpp, Data.cpp and main.cpp, it will say



        
相关标签:
3条回答
  • 2021-01-22 17:42

    You need to use include guards, or the easiest:

     #pragma once
    

    in your header files

    See Purpose of Header guards for more background

    Idea: 1.hpp

    #ifndef HEADER_GUARD_H1_HPP__
    #define HEADER_GUARD_H1_HPP__
    
    // proceed to declare ClassOne
    
    #endif // HEADER_GUARD_H1_HPP__
    
    0 讨论(0)
  • 2021-01-22 17:58

    In each of your header files write:

    #ifndef MYHEADERNAME_H
    #define MYHEADERNAME_H
    
    code goes here....
    
    #endif
    
    0 讨论(0)
  • 2021-01-22 18:03

    Its better like this:

    #ifndef DATA_H    /* Added */
    #define DATA_H    /* Added */
    
    #include <iostream>
    #include <string>
    
    // using namespace std;  /* Removed */
    
    class Data
    {
    private:
       std::string sType;
    public:
       Data();
       Data( std::string const& );          // Prevent copy of string object.
       void setSType( std::string& );       // Prevent copy of string object.
       std::string const& getSType() const; // prevent copy on return
       std::string& getSType();             // prevent copy on return
    };
    
    #endif /* DATA_H */
    

    The big fix is adding ifndef,define,endif. The #include directive works as if copying and pasting the .h to that line. In your case the include from main.cpp are:

       main.cpp
         -> Data.h  (1)
         -> Point.h
            -> Data.h (2)
    

    At (2), Data.h has already been `pasted' into main.cpp at (1). The class declaration of Data, i.e. "class Data{ .... };" , appears twice. This is an error.

    Adding include guards to the top and bottom of every .h are standard practice to avoid this problem. Don't think about it. Just do it.

    Another change I'd suggest is to remove any "using namespace ..." lines from any .h . This breaks the purpose of namespaces, which is to place names into separate groups so that they are not ambiguous in cases where someone else wants an object or function with the same name. This is not an error in your program, but is an error waiting to happen.

    For example, if we have:

    xstring.h:

    namespace xnames
    {
        class string
        {
            ...
        };
    }
    

    Foo.h

    #include <xstring>
    using namespace xnames;
    ...
    

    test.cxx:

    #include "Foo.h"  
    #include "Data.h"    // Breaks at:   Data( string );  -- std::string or xnames::string?
    
    ...
    void test()
    {
       string x;  // Breaks.  // std::string or xnames::string?
    }
    

    Here the compiler no longer knows whether you mean xnames::string or std::string. This fails in test.cxx, which is fixable by being more specific:

    void test()
    {
       std::string x;
    }
    

    However, this compilation still now breaks in Data.h. Therefore, if you provide that header file to someone, there will be cases when it is incompatible with their code and only fixable by changing your header files and removing the "using namespace ...;" lines.

    Again, this is just good coding style. Don't think about it. Just do it.

    Also, in my version of Data.h, I've changed the method parameters and return types to be references (with the &). This prevents the object and all of its state from being copied. Some clever-clogs will point our that the string class's is implementation prevents this by being copy-on-write. Maybe so, but in general, use references when passing or returning objects. It just better coding style. Get in the habit of doing it.

    0 讨论(0)
提交回复
热议问题