簡體   English   中英

C++中構造函數重載的問題

[英]Problem with constructor overloading in C++

我在類 NewsArticle 中重載構造函數時遇到困難。 實際上代碼工作正常,但我不知道為什么當我刪除這一行NewsArticle(Category c1, const char *title="untitled")的參數 title 的值並讓它像這樣NewsArticle(Category c1, const char *title) ?

#include<iostream>
#include<cstring>
using namespace std;

class Category {
private:
    char name[20];
public:
    Category() {
        //cout<<"Category default constructor"<<endl;
          strcpy(name,"unnamed");
    }
    Category(char *name) {
        //cout<<"Category constructor"<<endl;
        strncpy(this->name,name,20);
        this->name[19]=0;
    }
    Category(const Category &n) {
        strcpy(name,n.name);
        //cout<<"Category copy-constructor"<<endl;
    }
    void print() {
        cout<<"Category: "<<name<<endl;
   }
};

class NewsArticle {
private:
    Category c1;
    char title[30];
public:
    NewsArticle() {
        strcpy(title,"untitled");
        //cout<<"NewsArticle default constructor"<<endl;
    }
    NewsArticle(Category c1, const char *title="untitled") {
        //cout<<"NewsArticle constructor"<<endl;
        this->c1=c1;
        strncpy(this->title,title,30);
        this->title[29]=0;
    }
    NewsArticle(const NewsArticle &n) {
        //cout<<"NewsArticle copy-constructor"<<endl;
        strcpy(title,n.title);
        c1=n.c1;
    }
    void print() {
        cout<<"Article title: "<<title<<endl;
        c1.print();
    }
};


class FrontPage {
private:
    NewsArticle n1;
    float price;
    int editionNumber;
public:
    FrontPage() {
        //cout<<"Front Page default constructor"<<endl;
        price=0;
        editionNumber=0;
    }
    FrontPage(NewsArticle n1, float price=0, int editionNumber=0) {
        //cout<<"Front Page constructor"<<endl;
        this->n1=n1;
        this->price=price;
        this->editionNumber=editionNumber;
    }
    void print() {
        cout<<"Price: "<<price<<", Edition number: "<<editionNumber<<endl;
        n1.print();
    }
};


int main() {
char categoryName[20];
char articleTitle[30];
float price;
int editionNumber;

int testCase;
cin >> testCase;


if (testCase == 1) {
    int iter;
    cin >> iter;
    while (iter > 0) {
        cin >> categoryName;
        cin >> articleTitle;
        cin >> price;
        cin >> editionNumber;
        Category category(categoryName);
        NewsArticle article(category, articleTitle);
        FrontPage frontPage(article, price, editionNumber);
        frontPage.print();
        iter--;
    }
} else if (testCase == 2) {
    cin >> categoryName;
    cin >> price;
    cin >> editionNumber;
    Category category(categoryName);
    NewsArticle article(category);
    FrontPage frontPage(article, price, editionNumber);
    frontPage.print();
}// test case 3
else if (testCase == 3) {
    cin >> categoryName;
    cin >> articleTitle;
    cin >> price;
    Category category(categoryName);
    NewsArticle article(category, articleTitle);
    FrontPage frontPage(article, price);
    frontPage.print();
} else {
    FrontPage frontPage = FrontPage();
    frontPage.print();
}
return 0;
}

我不知道為什么當我刪除這一行 NewsArticle(Category c1, const char *title="untitled") 中的參數 title 的值並讓它像這樣 NewsArticle(Category c1, const char *title) 時它顯示錯誤?

在你的代碼中

   NewsArticle article(category);

您正在傳遞category但 NewsArticle 中的構造函數只有以下類型

   NewsArticle();
   NewsArticle(Category c1, const char *title);
   NewsArticle(const NewsArticle &n);

所以沒有匹配的原型可以調用,但如果你有默認參數

   NewsArticle(Category c1, const char *title="untitled")

NewsArticle article(category); 將這樣稱呼或這樣稱呼

   NewsArticle article(category,"untitled");

將調用NewsArticle(Category c1, const char *title);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM