不确定如何使用我的类和命名空间创建对象?c++

Not Sure how to create object with my class and namespace? c++

本文关键字:命名空间 创建对象 c++ 何使用 我的 不确定      更新时间:2023-10-16

我已经编码了一个类大片和命名空间cs52。创建对象的正确方法是吗

1) cs52::大片b(100,《兰博》,《史泰龙》);

2.)std::大片b(100,《兰博》,《史泰龙》);

还是其他什么?

代码:

#include <string>
using namespace std;
namespace cs52 {
class Blockbuster{
public:
Blockbuster( int length, string title, string star );
friend bool equal( const Blockbuster & b1, const Blockbuster & b2 );
int getLength();
string getTitle();
string getPlace();
void setTitle( string title );
void setPlace( string place );
void setLength( int length );
private:
int my_Length; // feature length in minutes
string my_Title; // feature title
string my_Star; // leading actor or actress
};
}

此外,我实际上还没有定义我的c++文件函数。将实现为boolBlockbuster::相等(const Blockbuster&b1,const Blockbuster&b2)

还是我需要在声明中加入朋友?

using namespace std不受欢迎(被认为是不良做法),因为它会导致冲突。我建议你去掉那条线。要实例化对象,请使用cs52::Blockbuster。您永远不会在编写的类上使用std::,因为这是标准库的命名空间。您可能还想添加一些更具体的命名空间指令,如using std::string。请注意,这是我自己的做事方式,当涉及到C++和名称空间时,每个人都有不同的意见。例如,在头文件中声明名称空间有一些危险,因为它可能会意外地更改包含名称空间的代码的含义

回答您的第二个问题:

您只能在声明中使用friend关键字,而不能在定义/实现中使用。(您将函数声明为友元函数)。