【问题标题】:Default Constructor error (C++)默认构造函数错误 (C++)
【发布时间】:2012-11-29 19:38:17
【问题描述】:

我已经编写了一个模板链接列表,但是当我尝试向其中添加一个对象时,我从节点构造函数中收到错误“C2512 'Customer': No Appropriate default constructor available”。

代码:

#pragma once
#include <iostream>
using namespace std;

template <class T>
class node;

template <class T>
class l_list
{

public:
    l_list() { head = tail = NULL; }
    ~l_list();
    void add(T &obj);
    T remove(int ID);
    void print(ostream &out);

private:
    node<T> *head, *tail;
};

template <class T>
class node
{
public:
    template<class> friend class l_list;
    node() {next = NULL;}
private:
    T data;
    node *next;
};

template <class T>
l_list<T>::~l_list()
{
}

template <class T>
void l_list<T>::add(T &obj)
{
    node<T> *ptr = new node<T>;
    ptr -> data = obj;
    ptr -> next = head;
    head = ptr;

    if (tail == NULL) {tail = ptr;}
}

template <class T>
T l_list<T>::remove(int ID)
{
    int i = 0;
    node<T> * ptr = head;

    while (ptr -> data -> id != ID)
    {
        ptr = ptr -> next;
    }
}

template <class T>
void l_list<T>::print(ostream &out)
{
    int i = 0;
    node<T> *ptr = head;
    while ( ptr != NULL )
    {
        out << ptr -> data << endl;
        ptr = ptr -> next;
        i++;
    }
}

以及我尝试放入列表中的对象

l_list<Customer> customers;
Customer bob("Bob", "25 Bob Lane", "01bob82", "M", "bob/bob/bob");
customers.add(bob);

编辑以添加客户:

#pragma once
#include "l_list.h"
#include "Account.h"
#include <string>

using namespace std;

class Customer
{
private:
    l_list<Account> accounts;
    string name;
    string address;
    string telNo;
    string sex;
    string dob;

public:
    Customer(string name, string address, string telNo, string sex, string dob)
    {
        Customer::name = name;
        Customer::address = address;
        Customer::telNo = telNo;
        Customer::sex = sex;
        Customer::dob = dob;
    }

    void createAccount()
    {
        cout << "What type of account?";

    }

    ~Customer()
    {
    }
};

【问题讨论】:

  • 你能显示Customer吗?
  • 您可能没有在Customer 中包含默认构造函数,而现在node 正在尝试默认构造您的data 成员。
  • 包括这个,以防万一:Customer() {}
  • 我觉得有必要指出,如果你使用类似int 而不是Customer,这应该可以工作。
  • @chris 它适用于任何支持默认构造的东西,不是吗?

标签: c++ constructor compiler-errors


【解决方案1】:

这一行:

node<T> *ptr = new node<T>

正在尝试在node 中默认构造一个T。由于没有默认构造函数,所以没有compiley =P

您可以通过使用复制构造函数(您定义或使用默认构造函数,只要T 正确符合The Rule of Three)或为T 定义默认构造函数来解决此问题。如果我想加强对T的构造访问,我更喜欢前者。

node<T> *ptr = new node<T>(obj);

当然,你需要为node&lt;T&gt;::node(const T&amp;)定义一个合适的构造函数

class node
{
public:
    template<class> friend class l_list;

    // T-copy-ctor-based constructor
    node(const T& obj) : data(obj), next(NULL) {};

private:
    T data;
    node *next;
};

早上很慢,如果我把里面的东西搞砸了,很抱歉。 =P

【讨论】:

  • 非常感谢,完美解决了。
  • @user1864298 很高兴它有帮助。
【解决方案2】:

问题出在 void l_list::add(T &obj) 的函数体上,因为如果你使用 node 的默认构造函数,它也会使用成员的默认构造函数,即 T 数据,这显然是没有定义的。不要使用 node 的默认构造函数,而是定义一个 node 构造函数,它接受一个参数 T& obj 并使用引用 obj 初始化成员数据。如果你只使用指向链表中节点的指针,它应该可以工作。

【讨论】:

    【解决方案3】:

    投诉的原因是您的node 班级中的T data;。 当您调用add 时,它会尝试构造一个T,即CustomerCustomer 没有没有参数的构造函数,所以它不起作用。

    你可以:

    1. 添加默认构造函数(无参数)
    2. 添加赋值运算符,以便您可以正确赋值给data

    • add 中的obj 传递到node 的构造函数中并正确初始化。

    • data 成为指针并分配新的Customer 对象以添加。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多