[英]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 do this very successfully if you just take care of these few tips 如果你只是处理这几个提示,你可以非常成功地做到这一点
Rodrigo already addressed your main problem. 罗德里戈已经解决了你的主要问题。 Let me just add some style comment:
让我添加一些风格评论:
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文件的其他文件继承。 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... using namespace std
指令,则会导致编译错误... <iostream>
, since it's already included in the header... Yes, <iostream>
has for sure a header guard, so it's not really included twice. <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.