您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> C/C++ >> VC,C2027错误!

VC,C2027错误!

来源:网络整理     时间:2016/9/2 12:30:03     关键词:

关于网友提出的“ VC,C2027错误!”问题疑问,本网通过在网上对“ VC,C2027错误!”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: VC,C2027错误!
描述:

用VC6.0调试《C++编程思想》上的一个例子,报错error C2027: use of undefined type 'Stack<><>,class std::allocator > >',请问什么原因,改怎么改?
谢谢!
//: C16:TStack2.h
#ifndef TSTACK2_H
#define TSTACK2_H
template 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& 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 Stack::~Stack() {
  while(head)
    delete pop();
}
template T* Stack::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 
#include 
#include 
using namespace std;
int main() {
  ifstream file("TStack2Test.cpp");
  assure(file, "TStack2Test.cpp");
  Stack 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::iterator it = textlines.begin();
  Stack::iterator* it2 = 0;
  while(it != textlines.end()) {
    cout << it->c_str() << endl;
    it++;
    if(++i == 10) // Remember 10th line
      it2 = new Stack::iterator(it);
  }
  cout << (*it2)->c_str() << endl;
  delete it2;
  return 1;
} ///:~


解决方案1:

引用 3 楼  的回复:
vc6.0对模板的基本特性还是支持的。lz的错误很容易解决,只需要调整头文件包含的顺序,
如下:
C/C++ code
#include    // 该头文件必须放置在“TStack2.h”之前
#include "TStack2.h"
#include "require.h"
#include 
#include 

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

lz是不是把模板类Stack的定义与实现放在不同的文件中了?vc还不支持这一特性,把Stack的定义与实现都放置到头文件TStack2.h中试一试。

解决方案3:

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


#include    // 该头文件必须放置在“TStack2.h”之前
#include "TStack2.h"
#include "require.h"
#include 
#include 
解决方案4:

引用 1 楼  的回复:
VC6对标准支持不好。升级编译器

sp.......
vc6..... 解决方案5:

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


以上介绍了“ VC,C2027错误!”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3718456.html

相关图片

相关文章