当前位置: 首页 > 知识库问答 >
问题:

LNK2019:未解析的外部符号;弗雷奇。h FE_迭代器()

卫招
2023-03-14

此链接器错误在主目录中。cpp和在foreach中。h级。构造函数和解构器都被标记为错误。

LNK2019:未解析的外部符号

“public:u thiscall FE_Iterator::~FE_Iterator(void)”在函数中引用“public:u thiscall Set,类std::allocator”

"公共:__thiscallFE_Iterator::FE_Iterator(无效)"引用函数"私有:__thiscall集,类std::allocator

森林. h

/*
 * File: foreach.h
 * Last modified on Thu Jun 11 12:04:09 2009 by eroberts
 * -----------------------------------------------------
 * This interface defines the foreach keyword, which is used to
 * simplify iteration.  All iterable classes import this interface,
 * so clients never need to do so explicitly.
 */

#ifndef _foreach_h
#define _foreach_h

#include "genlib.h"

/* These #includes are for files that contain "in" as a token */

#include <ios>
#include <fstream>
#include <sstream>

/* Redefine the ios constants (one of which is "in") */

static const ios::openmode IOS_APP = ios::app;
static const ios::openmode IOS_ATE = ios::ate;
static const ios::openmode IOS_BINARY = ios::binary;
static const ios::openmode IOS_IN = ios::in;
static const ios::openmode IOS_OUT = ios::out;
static const ios::openmode IOS_TRUNC = ios::trunc;

/*
 * Class: FE_Iterator
 * ------------------
 * This class is a base class for all Iterators that can work with
 * the foreach construct.  The only purpose of this class is to make
 * it possible to freeing the iterators after they are no longer needed.
 *
 * Note: FE_Iterator is implemented in lexicon.cpp, which is the only
 * iterable class that is not a template class.
 */

class FE_Iterator {
public:
    FE_Iterator();
    ~FE_Iterator();
};

/*
 * Class: FE_State
 * ---------------
 * This class is used to maintain the state of the foreach processing.
 * The class itself is essentially private, but the implementations in
 * the different classes use the fields directly.
 *
 * Note: FE_State is implemented in lexicon.cpp, which is the only
 * iterable class that is not a template class.
 */

class FE_State {
public:
    int state;
    FE_Iterator *iter;

    FE_State();
    ~FE_State();
};

/*
 * Macro: foreach
 * Usage: foreach (type var in collection) { . . . }
 * -------------------------------------------------
 * Provides a much simpler hook to the iterator facility.
 */

#define foreach(arg) \
  for (FE_State _fe; _fe.state < 2; ) \
    for (arg.foreachHook(_fe); _fe.state++ == 1; _fe.state = 0)

#define in =

#endif

主要的cpp

#include "stdafx.h"

#include <cstdlib>
#include <string>
#include <iostream>
#include <set>
#include <fstream>

#include "genlib.h"
#include "strutils.h"
#include "simpio.h"
#include "set.h"
#include "lexicon.h"
#include "iterator.h"
#include "foreach.h"

using namespace std;

void PrintRegExpMatches(string exp, Set<string> & matches)
{
    cout << "Activity codes that match " << exp << endl;
    cout << "--------------------------------------" << endl;

    Set<string>::Iterator it = matches.iterator();
    while (it.hasNext()) cout << it.next() << endl;

void PrintCorrections(string seed, int editDistance,
                      Set<Lexicon::CorrectionT> & matches)
{
    cout << "Activity codes that are within " << editDistance << " edits of " << seed << endl;
    cout << "--------------------------------------" << endl;

    Set<Lexicon::CorrectionT>::Iterator it = matches.iterator();
    while (it.hasNext()) {
        Lexicon::CorrectionT corr = it.next();
        cout << corr.suggestedWord << " is a distance of " << corr.editDistance;
        cout << " away." << endl;

共有2个答案

姚星河
2023-03-14

这是一个奇怪的巧合,但我和Eric Roberts合作开发了一个更新版本的foreach代码,它不需要单独的。cpp文件。文件的新版本就在这里,并且应该在编译时不会出现任何链接器错误:

#ifndef Foreach_Included
#define Foreach_Included

#include <iterator>
#include <map>
#include <cstddef>
#include <cstring>

/* These #includes are for files that contain "in" as a token */

#include <ios>
#include <fstream>
#include <sstream>
using namespace std;

/* Redefine the ios constants (one of which is "in") */

static const ios::openmode IOS_APP = ios::app;
static const ios::openmode IOS_ATE = ios::ate;
static const ios::openmode IOS_BINARY = ios::binary;
static const ios::openmode IOS_IN = ios::in;
static const ios::openmode IOS_OUT = ios::out;
static const ios::openmode IOS_TRUNC = ios::trunc;

/* Private implementation namespace */

namespace _fe {
   struct Range {
      virtual ~Range() { };
   };

   template <typename T>
   struct ArrayRange : Range {
      ArrayRange(const T *begin, const T *end) : iter(begin), end(end) { }
      const T *iter;
      const T *end;
   };

   template <typename CType>
   struct CRange : Range {
      CRange(const CType& c) :
         cont(c), iter(cont.begin()), end(cont.end()) { }
      CType cont;
      typename CType::iterator iter, end;
   };

   template <typename KT, typename VT, typename CT, typename AT>
   struct MapRange : Range {
      MapRange(const map<KT,VT,CT,AT> & c) :
         cont(c), iter(cont.begin()), end(cont.end()) { }
      map<KT,VT,CT,AT> cont;
      typename map<KT,VT,CT,AT>::iterator iter, end;
   };

/*
 * The State struct glues together all of these pieces and
 * stores all of the information throughout the loops.
 */

   struct State {
      State() : state(0), itr(NULL) { }
      ~State() { delete itr; }
      int state;
      Range *itr;
   };

/* General hook function */

   template <typename DowncastType, typename ValueType>
   ValueType HookImpl(State& fe) {
      DowncastType *ip = (DowncastType *) fe.itr;
      if (ip->iter == ip->end) {
         fe.state = 2;
         return ValueType();
      }
      fe.state = 1;
      ValueType vp = *ip->iter;     /* Subtle implementation note:    */
      ++ip->iter;                   /* Using *ip->iter++ here would   */
      return vp;                    /* require copying the iterator.  */
   }

/* Foreach implementation for containers */

   template <typename CType>
   CRange<CType> *Init(State & fe, const CType & collection) {
      fe.itr = new CRange<CType>(collection);
      return (CRange<CType>*) fe.itr;
   }

   template <typename CType>
   typename iterator_traits<typename CType::iterator>::value_type
   Hook(State & fe, CRange<CType> *) {
      return HookImpl<CRange<CType>,
         typename iterator_traits<typename CType::iterator>::value_type>(fe);
   }

/* For maps */

   template <typename K, typename V, typename C, typename A>
   MapRange<K,V,C,A> *Init(State & fe, const map<K,V,C,A> & collection) {
      fe.itr = new MapRange<K,V,C,A>(collection);
      return (MapRange<K,V,C,A>*) fe.itr;
   }

   template <typename DowncastType, typename ValueType>
   ValueType MapHookImpl(State & fe) {
      DowncastType *ip = (DowncastType *) fe.itr;
          if (ip->iter == ip->end) {
         fe.state = 2;
         return ValueType();
      }
      fe.state = 1;
      ValueType key = ip->iter->first;
      ++ip->iter;
      return key;
   }

   template <typename K, typename V, typename C, typename A>
   K Hook(State & fe, MapRange<K,V,C,A> *) {
      return MapHookImpl<MapRange<K,V,C,A>,K>(fe);
   }

/* For C strings */

   template <size_t n>
   ArrayRange<char> *Init(State & fe, char (&str)[n]) {
      fe.itr = new ArrayRange<char>(str, str + strlen(str));
      return (ArrayRange<char>*) fe.itr;
   }

   template <size_t n>
   ArrayRange<char> *Init(State & fe, const char (&str)[n]) {
      fe.itr = new ArrayRange<char>(str, str + strlen(str));
      return (ArrayRange<char>*) fe.itr;
   }

/* For arrays */

   template <typename T, size_t n>
   ArrayRange<T> *Init(State & fe, T (&arr)[n]) {
      fe.itr = new ArrayRange<T>(arr, arr + n);
      return (ArrayRange<T>*) fe.itr;
   }

   template <typename T, size_t n>
   ArrayRange<T> *Init(State & fe, const T (&arr)[n]) {
      fe.itr = new ArrayRange<T>(arr, arr + n);
      return (ArrayRange<T>*) fe.itr;
   }

   template <typename T>
   T Hook(State& fe, ArrayRange<T>*) {
      return HookImpl<ArrayRange<T>, T>(fe);
   }

}

/* The actual foreach and in macros */

#define foreach(arg) \
   for (_fe::State _fe; _fe.state < 2; ) \
      for (arg)); _fe.state++ == 1; _fe.state = 0)

#define in = _fe::Hook(_fe, _fe.state != 0 ? NULL : _fe::Init(_fe,

#endif

希望这有帮助!

陆浩博
2023-03-14

很简单。您已声明但未定义FE_Iterator()的构造函数和析构函数。即使它是基类,也应该定义它们。

 类似资料:
  • 首先,我知道这个问题在这个网站上到处都是,但是我看了几乎所有的问题,似乎找不到问题所在。这是在VS 2012中。谢谢。 这是插座。cpp公司 这是stdafx。h类 这是我的错误消息:

  • 我得到了这个错误,但我不知道如何修复它。 我正在使用Visual Studio 2013。我将解决方案命名为MyProjectTest这是我的测试解决方案的结构: -功能。H -功能。cpp -main.cpp 我是初学者;这是一个简单的程序,运行时没有错误。我在互联网上阅读并对单元测试感兴趣,因此我创建了一个测试项目: 菜单文件→新建→项目...→已安装→模板→Visual C→测试→本地单元测

  • 我有一个cpp文件,其中包括: 当尝试运行AcquireCredentialsHandleW时,我得到.. MSDN说它在sspi.h中。

  • 我在我的MFC vtk项目中使用Visual Studio 2008。我收到一个链接错误,如下所示: vtkPBFEAView。obj:警告LNK4217:本地定义的符号?DrawDC@vtkMFCWindow@@QAEXPAVCDC@@@Z(public:void _thiscall vtkMFCWindow::DrawDC(类CDC*))在函数“protected:virtual void _

  • 当我运行下面的简单代码时,我有两个错误,如下所示: 错误: msvcrtd.lib(crtexew.obj):错误LNK2019:函数中引用了未解析的外部符号 我该怎么办?

  • 如果在中发现未解析的外部符号,我必须做什么。库文件。如果我在中发现未解析的外部符号。c或。cpp我能修好它。但当我在里面发现了未解决的外部符号。lib(library)文件如何解决这个问题? 在我的例子中,我发现了如下链接器错误: 关于这一点的任何建议都将是有益的。