简体   繁体   English

c ++如何正确地将.cpp拆分为.cpp和.h

[英]c++ How to correctly split a .cpp into a .cpp and a .h

I'm really new to splitting a .cpp into a .cpp and a .h. 我真的很擅长将.cpp分成.cpp和.h。

I've used .h files before, but never really split a .cpp into a .cpp and a .h. 我之前使用过.h文件,但从未真正将.cpp分成.cpp和.h。

I know .h files are only for declarations and .cpp are for definitions, and I attempted to split a .cpp into a .cpp and a .h but I get a lot of errors, so I was wondering if someone can help me with my code. 我知道.h文件仅用于声明而.cpp用于定义,我试图将.cpp拆分为.cpp和.h但我收到很多错误,所以我想知道是否有人可以帮助我我的代码。

The following class is the class that has NOT yet been split into a .cpp and a .h yet, just to show you guys the "before version". 以下类是尚未拆分为.cpp和.h的类,只是为了向您展示“之前的版本”。

TicketOrder.cpp TicketOrder.cpp

#include <iostream>

using namespace std;

class TicketOrder
{

private :

        char type;
        int quantity;

public :

          friend std::ostream& operator<<(std::ostream& os, TicketOrder const& order)
         {
                 os << " Type: " << order.type << ", Quantity: " << order.quantity;
                 return os;
         }


        //Getters

        int getQuantity() const;
         {
                        return quantity;
         }

        char getType() const;
          {
                        return type;
          }

        //Setters    

        void setQuantity (int x)
                {
                        quantity =x;
                }

        void setType(char y)
                {
                        type =y;
                }   
};

Now, I will split that above class into a .cpp and a .h 现在,我将上面的类拆分为.cpp和.h

TicketOrder.cpp TicketOrder.cpp

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

class TicketOrder
{

        //Getters

        int getQuantity() const
                {
                        return quantity;
                }

        char getType() const
                {
                        return type;
                }

        //Setters

        void setQuantity (int x)
                {
                        quantity =x;
                }

        void setType(char y)
                {
                        type =y;
                }        
};

TicketOrder.h TicketOrder.h

#include <iostream>

using namespace std;

class TicketOrder
{

private :

        char type;
        int quantity;

public :

          friend std::ostream& operator<<(std::ostream& os, TicketOrder const& order)
        {
                 os << " Type: " << order.type << ", Quantity: " << order.quantity;
                 return os;
        }

        //Getters

        int getQuantity() const;    
        char getType() const;

        //Setters

        void setQuantity (int x);    
        void setType(char y);       
};

I have one other class that is used to contain the main class that I will not include in here because it's long and I do not think it's important because I know I'm doing the .h and .cpp wrong. 我还有一个用于包含我不会在这里包含的主类的类,因为它很长并且我认为它不重要因为我知道我正在做.h和.cpp错误。

When I try to compile the main, it gives me this error: 当我尝试编译main时,它给了我这个错误:

Undefined                       first referenced
 symbol                             in file
TicketOrder::getQuantity() const    /var/tmp//ccaSflFG.o
TicketOrder::setType(char)          /var/tmp//ccaSflFG.o
TicketOrder::setQuantity(int)       /var/tmp//ccaSflFG.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Can anyone help me with splitting the .h and the .cpp? 任何人都可以帮我拆分.h和.cpp吗? Or give me suggestions? 或者给我一些建议? I did a lot of research and can't figure out what's wrong. 我做了很多研究,无法弄清楚出了什么问题。

Thank you. 谢谢。

Your .cpp file is wrong because is re-declaring the class, so that's and error. 您的.cpp文件错误,因为重新声明了类,所以这是错误的。 It should be something along the lines of: 它应该是这样的:

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

//Getters
int TicketOrder::getQuantity() const
{
    return quantity;
}
char TicketOrder::getType() const
{
    return type;
}

//and so on

Also, note that adding a using namespace in a header file, as you are doing is, considered very bad style. 另外,请注意,在头文件中添加using namespace ,正如您所做的那样,被视为非常糟糕的样式。

You can play with lzz , it does exactly this automatically. 你可以玩lzz ,它会自动完成。 In it's default mode it will show you more or less where things usually go. 在它的默认模式下,它会向您展示或多或少的地方。

You can do this very successfully if you just take care of these few tips 如果你只是处理这几个提示,你可以非常成功地做到这一点

  1. Your .cpp files should contain your main function 您的.cpp文件应包含您的主要功能
  2. Your .h file should contain the header files 您的.h文件应包含头文件
  3. Your .cpp(ie your program) should declare all the headerfile names in the top 你的.cpp(即你的程序)应该在顶部声明所有头文件名
  4. Your .h file should contain any other headerfiles needed in the functions in the .h file 您的.h文件应包含.h文件中函数所需的任何其他头文件
  5. When you compile the .h file it may show some error but don't acre that. 当您编译.h文件时,它可能会显示一些错误,但不会出现这种情况。 It is because it does not contain the main function. 这是因为它不包含主要功能。

Rodrigo already addressed your main problem. 罗德里戈已经解决了你的主要问题。 Let me just add some style comment: 让我添加一些风格评论:

  • you don't need using namespace std , since you are already addressing the std members with the std:: prefix. 您不需要using namespace std ,因为您已经使用std::前缀寻址std成员。 In my opinion this is (almost) always the best solution, since the using namespace X directive could be inherited by some other files that include your .h file. 在我看来,这几乎总是最好的解决方案,因为using namespace X指令可以被包含.h文件的其他文件继承。
  • if you really want to use using namespace std , place that directive inside your class, so that other files that include yours won't have it. 如果你真的想using namespace std ,那么将该指令放在你的类中,这样包含你的其他文件就不会拥有它。 They might define functions/classes with the same name of the std ones, which would cause compilation errors if there is a using namespace std directive above... 他们可能会定义与std相同名称的函数/类,如果上面有一个using namespace std指令,则会导致编译错误...
  • your .cpp file does not need to include <iostream> , since it's already included in the header... Yes, <iostream> has for sure a header guard, so it's not really included twice. 你的.cpp文件不需要包含<iostream> ,因为它已经包含在标题中......是的, <iostream>肯定有一个标题保护,所以它实际上并没有包含两次。 But I think it's nice to not include what's already included in the header... But maybe it's just my taste... ;-) 但是我认为不包括已经包含在标题中的内容很好......但也许这只是我的口味... ;-)

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

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