首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

VC,C2027异常

2013-01-07 
VC,C2027错误!用VC6.0调试《C++编程思想》上的一个例子,报错error C2027: use of undefined type Stackcla

VC,C2027错误!
用VC6.0调试《C++编程思想》上的一个例子,报错error C2027: use of undefined type 'Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >',请问什么原因,改怎么改?
谢谢!

//: C16:TStack2.h
#ifndef TSTACK2_H
#define TSTACK2_H

template<class T> class Stack {
  struct Link {
    T* data;
    Link* next;
    Link(T* dat, Link* nxt)
      : data(dat), next(nxt) {}
  }* head;
public:
  Stack() : head(0) {}
  ~Stack();
  void push(T* dat) {
    head = new Link(dat, head);
  }
  T* peek() const { 
    return head ? head->data : 0;
  }
  T* pop();
  // Nested iterator class:
  class iterator; // Declaration required
  friend class iterator; // Make it a friend
  class iterator { // Now define it
    Stack::Link* p;
  public:
    iterator(const Stack<T>& tl) : p(tl.head) {}
    // Copy-constructor:
    iterator(const iterator& tl) : p(tl.p) {}
    // The end sentinel iterator:
    iterator() : p(0) {}
    // operator++ returns boolean indicating end:
    bool operator++() {
      if(p->next)
        p = p->next;
      else p = 0; // Indicates end of list
      return bool(p);
    }
    bool operator++(int) { return operator++(); }
    T* current() const {
      if(!p) return 0;
      return p->data;
    }
    // Pointer dereference operator:
    T* operator->() const { 
      require(p != 0, 
        "PStack::iterator::operator->returns 0");
      return current(); 
    }
    T* operator*() const { return current(); }
    // bool conversion for conditional test:
    operator bool() const { return bool(p); }
    // Comparison to test for end:
    bool operator==(const iterator&) const {
      return p == 0;
    }
    bool operator!=(const iterator&) const {
      return p != 0;
    }
  };
  iterator begin() const { 


    return iterator(*this); 
  }
  iterator end() const { return iterator(); }
};

template<class T> Stack<T>::~Stack() {
  while(head)
    delete pop();
}

template<class T> T* Stack<T>::pop() {
  if(head == 0) return 0;
  T* result = head->data;
  Link* oldHead = head;
  head = head->next;
  delete oldHead;
  return result;
}
#endif // TSTACK2_H ///:~
  


#include "stdafx.h"
//: C16:TStack2Test.cpp
#include "TStack2.h"
#include "require.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
  ifstream file("TStack2Test.cpp");
  assure(file, "TStack2Test.cpp");
  Stack<string> textlines;
  // Read file and store lines in the Stack:
  string line;
  while(getline(file, line))
    textlines.push(new string(line));
  int i = 0;
  // Use iterator to print lines from the list:
  Stack<string>::iterator it = textlines.begin();
  Stack<string>::iterator* it2 = 0;
  while(it != textlines.end()) {
    cout << it->c_str() << endl;
    it++;
    if(++i == 10) // Remember 10th line
      it2 = new Stack<string>::iterator(it);
  }
  cout << (*it2)->c_str() << endl;
  delete it2;
  return 1;
} ///:~

[解决办法]
VC6对标准支持不好。升级编译器
[解决办法]

引用:
VC6对标准支持不好。升级编译器


sp.......

vc6.....
[解决办法]
vc6.0对模板的基本特性还是支持的。lz的错误很容易解决,只需要调整头文件包含的顺序,
如下:

#include <string>   // 该头文件必须放置在“TStack2.h”之前
#include "TStack2.h"
#include "require.h"
#include <iostream>
#include <fstream>

[解决办法]
lz是不是把模板类Stack的定义与实现放在不同的文件中了?vc还不支持这一特性,把Stack的定义与实现都放置到头文件TStack2.h中试一试。
[解决办法]
引用:
vc6.0对模板的基本特性还是支持的。lz的错误很容易解决,只需要调整头文件包含的顺序,
如下:
C/C++ code

#include <string>   // 该头文件必须放置在“TStack2.h”之前
#include "TStack2.h"
#include "require.h"
#include <iostream>
#include <fstream>


不好意思,头文件<string>不是必须要放置在“TStack2.h”之前,顺序可前可后。只要在使用Stack<string>的时候编译器能够看到string的定义就行。

热点排行