0 c++
我为什么得到一个帮助'C2011 'Transaction':'class' type redefinition?我确信这显然很明显,但我不能为我的生活弄清楚.请帮忙.
transaction.h
#include <string>
class Transaction
{
private:
int amount;
std::string type;
public:
Transaction(int amt, std::string kind);
std::string Report();
};
Run Code Online (Sandbox Code Playgroud)
transaction.cpp
#include "transaction.h"
using namespace std;
Transaction::Transaction(int amt, std::string kind):amount(amt), type(kind)
{
}
string Transaction::Report()
{
string report;
report += " ";
report += type;
report += " ";
report += to_string(amount);
return report;
}
Run Code Online (Sandbox Code Playgroud)
您可以在头文件中使用标头保护,这将确保您永远不会在任何其他cpp文件中定义类或结构等.
要添加标题保护,您只需执行以下操作:
#ifndef TRANSACTION_H
#define TRANSACTION_H
// your header file
#endif
Run Code Online (Sandbox Code Playgroud)
或者只是添加
#pragma once
Run Code Online (Sandbox Code Playgroud)
你所有的头文件,你很好.