English 中文(简体)
链接的列表复制器模板中的汇编错误
原标题:Compilation error in linked list iterator template

我新加入 C++ 模板。 编辑时出错 :

main.cpp(17) : error C2923:  Iterator  :
     myWords  is not a valid template type argument for parameter  T 

main.cpp(17) : error C2133:  iterator  : unknown size error C2512:
     Iterator  : no appropriate default constructor available

Code:

#include "MyList.h"
#include "MyList.cpp"
#include <string>
using namespace std;

int main()
{
    LinkedList<string> myWords;
    myWords.addAtBeginning("Data");

    Iterator<myWords> iterator;

    //iterator.advance();
    return 0;
}

页眉文件

      #ifndef MYLIST_H
#define MYLIST_H

template <class T>
class Iterator;

template <class T>
struct link
{
   link * previous;
   link * next;
   T s;
};

template <class T>
class LinkedList 
{
   link<T> * head;   // No longer a global variable 
 public:
   void addAtBeginning(T word);
   void printAll();
   void printString(link<T> *p);  // prints the string in the link pointed to by p.
   void deleteFromBeginning(); // deletes the first link in the list.    
   LinkedList();  // default constructor
   friend class Iterator<T>;
};

template <class T>
class Iterator
{
private:
   link<T> * p;

public:
    // construct the iterator by having it refer to a linked list
    Iterator(LinkedList<T> whatIamIterating);   

    // returns the string in the current link
    T current();                        

    // move the Iterator to the next link
    void advance();                          

    // returns true when we are finished with list
    int atEnd(); 
};

#endif

CPP 档案资料

      #include "MyList.h"

// Default constructor of linkedlist
template <class T>
LinkedList<T>::LinkedList()  
{
   head=0;
}

// Function to add at the beginning
template <class T>
void LinkedList<T>::addAtBeginning(T word)
{
   link<T> * tmp;
   tmp = new link<T>;
   tmp->s = word;
   tmp->next=head;
   tmp->previous=0;
   head=tmp;
}

// Function to print the all strings
template <class T>
void LinkedList<T>::printAll()
{
   link<T> * tmp;
   tmp = head;
   while (tmp != 0)
   {
      cout << tmp->s;
      tmp = tmp->next;
   }
   cout << endl;
}

// Prints the string in the link pointed to by p.
template <class T>
void LinkedList<T>::printString(link<T> *p)
{                          
    cout << p->s << endl;
}

// Deletes the first link in the list.
template <class T>
void LinkedList<T>::deleteFromBeginning()
{
    link<T> *oldhead = head;                    
    head = head->next;

    delete oldhead;
}

// Function definitions for Iterator class
// Constructor for iterator class
template <class T>
Iterator <T>::Iterator(LinkedList<T> whatIamIterating)
{
   p = whatIamIterating.head;
}

// Function to get the current string of the node
template <class T>
T Iterator<T>::current()
{
    return p->s;
}

template <class T>
void Iterator<T>::advance()
{
    p = p->next;
}

template <class T>
int Iterator<T>::atEnd()
{
    if (p == 0)
        return 1;
    else
        return 0;
}
问题回答

它必须是 Iterator< 链接的List< string> & gt; type name 是模板参数, 不是对象 。

Iterator<myWords> iterator;

您使用 < code> myWords 作为一种类型,但它是一个变量。 您想要的是一个变量。

Iterator<string> iterator(myWords);

sstring 为您用于 LinkedList 的类型 ( Solves first 编译器错误) 和 Indererator comberrcutor 采取 LinkedList <T> 对象(在您的情况下, T (在您的情况下) ( < strong > soolves 第二次编译器错误) ) 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签