如何在c ++ </template>中声明对象<template>

时间:2013-03-19 22:24:24

标签: c++ compiler-errors linked-list

我正在努力解决这个问题。错误发生在main.cpp的第58行,我在第58行写了一个明显的评论。

|58|error: expected primary-expression before ';' token

Main.cpp的

#include <iostream>
#include <vector>
#include "LinkedList.h"
using namespace std;

bool eic(const string &str1, const string &str2){
    if(str1.length() != str2. length())
       return false;
    for(int i = 0; i < str1.length(); i++)
        if(toupper(str1[i]) != toupper(str2[i])) return false;
    return true;
}

vector<string> tokenizer(const string &str, char delim, bool emptyok)
{
    vector<string> tokens;
    string t;

    for(int i = 0; i < str.length(); i++)
    {
        if (str[i] == delim)
        {
            if(emptyok || (t.length() != 0))
                tokens.push_back(t);

            t.clear();
            continue;
        }

        t.push_back(str[i]);
    }
    if(emptyok || (t.length() != 0)) tokens.push_back(t);
    return tokens;
}

int main(){
    LinkedList<int> sList;// = LinkedList<int>;
    string input;
    cout << "Type 'commands' to see the list of commands" << endl;
    cin >> input;
    vector<string> inputV = tokenizer(input,' ',false);
    while(!eic(input,"exit")){
        if(eic(input,"commands")){
            cout << endl;
            cout << "Do not include <> in any commands" << endl;
            cout << endl;
            cout << "Create <name of list>: Create a new list and names it." << endl;
            cout << "Print <name of list>: Prints out the entire list." << endl;
            cout << "Add <name of list> <item>: Adds an element to the list." << endl;
            cout << "Delete <name of list> <item>: Deletes an element from the list." << endl;
            cout << "DeleteAll <name of list> <item>: Deletes all occurences of the element from the list." << endl;
            cout << "MakeEmpty <name of list>: Removes all elements from the list." << endl;
            cout << "Length <name of list>: Tells you how many elements are in the list" << endl;
            cout << "Remove <name of list> deletes an entire list" << endl;
            cout << "Exit: Terminates the program" << endl;
        }
        else if(eic(inputV[0],"create")){
            sList = LinkedList<int>; // LINE 58 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            sList.setName(inputV[1]);
            cout << sList.getName();
            //cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"print")){
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"add")){
            //sList->insertItem(9);
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"delete")){
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"deleteAll")){
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"makeEmpty")){
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"length")){
            cout << "This function is still under construction" << endl;
        }
        else if(eic(inputV[0],"remove")){
            cout << "This function is still under construction" << endl;
        }
        else cout << endl << "Invalid inquiry, please enter 'commands' to see a list of valid commands." << endl;
        cin >> input;
    }
}

如果你需要它,我的LinkedList.cpp文件

#include <iostream>
#include "LinkedList.h"

using namespace std;

template <class xtype>
LinkedList<xtype>::LinkedList()
{
    cout << "List created successfuly\n";
}

template <class xtype>
void LinkedList<xtype>::setLength(int x){
    length = x;
}

template <class xtype>
int LinkedList<xtype>::getLength(){
    return length;
}

template <class xtype>
void LinkedList<xtype>::setName(string x){
    name = x;
}

template <class xtype>
string LinkedList<xtype>::getName(){
    return name;
}

template <class xtype>
void LinkedList<xtype>::insertItem(xtype item){
    node<xtype> *temp = new node<xtype>;
    if(head == NULL || head->info > item){
        temp->next = head;
        head = temp;
    }
    else{
        node<xtype> *q = head;
        node<xtype> *p = head->next;

        while(p != head && p->info <= item){
            q = p;
            p = p->next;
        }
        q->next = temp;
        temp->next = p;
    }
}

template class LinkedList<int>;

和LinkedList头文件

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;

template <class xtype>
struct node{
    xtype info;
    node *next;
    node *prev;
};

template <class xtype>
class LinkedList
{
    public:
        LinkedList();
        int getLength();
        void setLength(int);
        void setName(string);
        string getName();
        //bool searchItem(xtype item);
        void insertItem(xtype item);
        //void deleteItem(xtype item);
        //int numOccur(xtype item);
    protected:
    private:
        node<xtype> *head;
        node<xtype> *term;
        int length;
        string name;
};

#endif // LINKEDLIST_H

您可以给予我任何帮助,我将不胜感激。我是c ++的新手,来自java,直到现在我才一直在为这一切做好准备。

1 个答案:

答案 0 :(得分:4)

当您将sList声明为LinkedList<int>时,您已经调用了默认构造函数来初始化sList。没有必要明确地分配这个,就像你(尝试)在失败的行中那样。

这证明了从Java到C ++时的一个令人困惑但至关重要的概念:RAII