我开始模板LinkedList类的基本实现。当我用g ++单独编译头文件时,没有问题。但是,当我尝试使用另一个实现某种类型链表的类进行编译时,它给了我一个错误。它似乎想编译节点类型的对象。为什么会这样做? Node构造函数不调用模板类型的构造函数。当使用新关键字时,模板类的构造函数调用类型构造函数
我将发布所有相关的头文件。可能有无关的代码,但我想显示正在使用的内容。我将发布错误消息,然后是链接列表实现,然后是使用链接列表的类,然后是错误中提到的ParkingLot构造函数。
以下是错误消息:
LinkedList.h: In instantiation of ‘Node<T>::Node(T) [with T = ParkingLot]’:
LinkedList.h:46:9: required from ‘void LinkedList<Type>::addNode(Type) [with Type = ParkingLot]’
Decal.h:20:28: required from here
LinkedList.h:13:16: error: no matching function for call to ‘ParkingLot::ParkingLot()’
Node(T input) {
^
LinkedList.h:13:16: note: candidates are:
In file included from Decal.h:2:0,
from test.cpp:3:
ParkingLot.h:28:2: note: ParkingLot::ParkingLot(int, std::string, int, int, int, int)
ParkingLot(int num_spaces, std::string name, int x, int y, int randSpacesPercent, int randLotPercent){
^
ParkingLot.h:28:2: note: candidate expects 6 arguments, 0 provided
ParkingLot.h:7:7: note: ParkingLot::ParkingLot(const ParkingLot&)
class ParkingLot {
^
ParkingLot.h:7:7: note: candidate expects 1 argument, 0 provided
ParkingLot.h:7:7: note: ParkingLot::ParkingLot(ParkingLot&&)
ParkingLot.h:7:7: note: candidate expects 1 argument, 0 provided
任何人都可以提供一些建议吗?我不知道为什么当我只想构造一个ParkingLot类型的节点时,它会尝试构造一个ParkingLot对象。
下面是执行:
#include <iostream>
template <typename T> class Node {
public:
/*Class variables*/
T data;
Node* next;
/*Class constructors*/
Node(T input) {
data = input;
next = NULL;
}
};
template <typename Type> class LinkedList {
public:
/*Class variables*/
Node<Type>* head;
/*Class constructor*/
LinkedList(){
head = NULL;
}
/*Class Methods*/
void addNode(Type value) {
Node<Type>* p;
if (head == NULL) {
head = new Node<Type>(value); ********ERROR
}
else {
p = head;
while (p->next != NULL){
p = p->next;
}
p->next = new Node<Type>(value); ************ERROR
}
}
//Check to see if linked list contains specified value
bool contains(Type value) {
Node<Type>* search;
if (head != NULL) {
search = head;
}
else {
return false;
}
while(search->next != NULL) {
if (search->data.compare(value)) {
return true;
}
search = search->next;
}
if (search->next == NULL && search->data.compare(value)) {
return true;
}
else {
return false;
}
}
void print(){
Node<Type>* p;
p = head;
while (p != NULL) {
std::cout << p->data.print() << " ";
p = p->next;
}
std::cout << "\n";
}
};
这里是刚刚尝试使用类型停车场的链表贴花类的代码。它是这样设计的,以便一定数量的停车场对应于一种贴花类型。
#include <string>
#include "ParkingLot.h"
#include "LinkedList.h"
class Decal {
//Class Variables
private:
LinkedList <ParkingLot> decal_list;
std::string decal_name;
//Class Methods
public:
Decal(std::string name) {
decal_name = name;
}
void addLot(ParkingLot newLot) {
decal_list.addNode(newLot);
}
bool hasLot(ParkingLot searchLot) {
return decal_list.contains(searchLot);
}
};
最后,我已经包括了停车场的构造函数作为参考。它有一个名称和位置x,y以及填充其空间的其他参数。
ParkingLot(int num_spaces, std::string name, int x, int y, int randSpacesPercent, int randLotPercent){
//Generate bool array for parking spaces
lot_capacity = num_spaces;
for (int i =0; i<lot_capacity; i++) {
parking_lot[i] = true;
}
/*Determine if lot is full or not, and if not generate random full spaces*/
//Assigning percentages to corresponding variable
sp_generator = randSpacesPercent;
lot_generator = randLotPercent;
//Make lot full or not based on percentage
generateLot(lot_generator);
//If lot is not full, assign certain percentage of spots as full/empty
if (isFull() == false) {
generateSpaces(sp_generator);
}
//Assing other vars
parking_lot_name = name;
x_locat = x;
y_locat = y;
}
谢谢你的帮助,你可以提供!
使用构造函数初始化列表来初始化数据成员 - '节点(T输入):数据(输入),未来(NULL){}'否则,你的'Node'构造函数将尝试默认构造'T' ,因为'ParkingLot'没有默认的构造函数,所以会失败。 – Praetorian 2014-12-04 02:13:37