如何使用类

时间:2012-09-30 23:57:44

标签: c++ class

我一直在学习c ++中的课程,我从一本古老的俄语书中得到了一些关于书类的代码,我尝试修改它并运行它不工作可能有些帮助我理解为什么authour使用这个代码( strdup做了什么?)

Author = strdup(autho);

在构造函数中,并且这行代码错误

Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");

任何有简单直接解释的人?

以下主要代码

using namespace std;

class Book{

    char * Author;
    char * Type;
    char * Title;
    int * Pages;
    unsigned int * Yearpublished;
    unsigned int  * Publishing;

    Book(char * autho, char * type, char * title,   int * pages, unsigned int * yearpublished, unsigned int  * publishing ){

        Author = strdup(autho);
        Type = strdup(type);
        Title = strdup(title);
        Pages = pages;
        Yearpublished = yearpublished;
        Publishing = publishing;

    }

    ~Book(){

        if(Author != NULL){

             free(Author);

        }
        if(Type != NULL){

            free(Type);
        }

        if(Title != NULL){

            free(Title);
        }
    }

};

int main(){

    cout << "main start" << endl;

    Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");

    cout << "main finish" << endl;
return 0;
}

3 个答案:

答案 0 :(得分:5)

发布的代码有很多很多问题。很多事情几乎每一行都是错误的。

最明显的一点是,您尝试将一年存储为int *,然后传入包含"12.11.13"字符串。这不起作用。你对pages做了同样的事情;接受int*并传入包含 int的字符串。你不能这样做,这不是指针的工作方式。你对指针的大多数使用表明你并不真正知道*的作用,你应该在引入一些非常很难找到你的错误之前停下来阅读指针。码。您应该将您的年份存储为字符串(非常糟糕的想法)或将其存储为unix time中的整数,这是非常标准的。

您应该删除using namespace std并将其替换为#include <string>。您应该丢弃以char*开头的所有行,并将其替换为std::string并抛弃int*行,并将其int

您还声明了一个私有构造函数和析构函数。您需要在成员变量声明之后但public:之前添加Book()。然后你应该抛弃构造函数的主体并使用初始化列表。

您还没有包含<iostream>,因此您的cout来电可能会导致错误。

完成上述操作后,您应该完全删除析构函数~Book()

例如:

class Book{

    std::string Author;
    // ...
    int Pages;
    // ...

    public:

    Book(std::string author, /* ... */ int pages /* ... */)
    : Author(author), Pages(pages) {

    }
};

答案 1 :(得分:3)

那里有很多错误。

1)你使用太多指针了。在这段代码中你甚至不需要一个指针。正如meagar所指出的,您可以将char*替换为std::string。您不需要指向整数或无符号整数的指针,您可以按原样使用该类型。

2)在调用构造函数时,您传递的是字符串(用引号“”表示)而不是数字。如果您想传递数字,请不要使用引号。

3)你正在使用“使用命名空间std”这是错误的,原因很多,并且已经在很多网站上解释了很多次,我会让你搜索原因。

4)您在free()未分配的内存上使用malloc()(由于我们有newdelete,因此在C ++中甚至不建议使用#include <iostream> #include <string> class Book { private: std::string Author, Type, Title, Publishing; unsigned int Pages, Yearpublished; public: Book(const char* autho, const char* type, const char* title, unsigned int pages, unsigned int yearpublished, std::string publishing ) { Author = strdup(autho); Type = strdup(type); Title = strdup(title); Pages = pages; Yearpublished = yearpublished; Publishing = publishing; } ~Book() { } }; int main() { std::cout << "main start" << std::endl; Book s("edgar", "science", "chemistry for dummies", 502, 2012,"1.12.96"); std::cout << "main finish" << std::endl; return 0; } )。

编辑:如果这是真正的代码,你已经从一本关于C ++的书中获取,那么请将本书用作启动器,因为它不值得更多。

这是一个用C ++编写的很多更简单的代码版本。

{{1}}

还有很多需要改进的地方,但这至少可以编译并运行得很好。

答案 2 :(得分:1)

我对你的问题感到有点困惑,但我认为你主要问的是strdup

它复制传递的字符串并返回指向新创建的jsut的指针。 在析构函数中,你破坏这些分配的字符串,如果有的话。