0

我无法在 MSVC 中解决此链接问题 LNK2001。

这个问题的根本原因是什么?是来自朋友班吗?

或者我以错误的方式调用了该函数。

我对 C++ 没有太多经验。请帮忙。

头文件:

namespace tesseract {

class Tesseract;    

class TESS_API LTRResultIterator : public PageIterator {
  friend class ChoiceIterator;
 public:

  LTRResultIterator(PAGE_RES* page_res, Tesseract* tesseract,
                    int scale, int scaled_yres,
                    int rect_left, int rect_top,
                    int rect_width, int rect_height);
  virtual ~LTRResultIterator();    

  char* GetUTF8Text(PageIteratorLevel level) const;      
};    

class ChoiceIterator {
 public:      
  explicit ChoiceIterator(const LTRResultIterator& result_it);
  ~ChoiceIterator();      
  bool Next();       
  const char* GetUTF8Text() const;      
  float Confidence() const;

 private:      
  WERD_RES* word_res_;      
  BLOB_CHOICE_IT* choice_it_;
};

}  // namespace tesseract.

#endif  // TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H__

.cpp 文件:

namespace tesseract {

LTRResultIterator::LTRResultIterator(PAGE_RES* page_res, Tesseract* tesseract,
                                     int scale, int scaled_yres,
                                     int rect_left, int rect_top,
                                     int rect_width, int rect_height)
  : PageIterator(page_res, tesseract, scale, scaled_yres,
                 rect_left, rect_top, rect_width, rect_height),
    line_separator_("\n"),
    paragraph_separator_("\n") {
}

LTRResultIterator::~LTRResultIterator() {
}    

char* LTRResultIterator::GetUTF8Text(PageIteratorLevel level) const {
  if (it_->word() == NULL) return NULL;  // Already at the end!
  ...
  return result;
}    

float LTRResultIterator::Confidence(PageIteratorLevel level) const {
  if (it_->word() == NULL) return 0.0f;  // Already at the end!
  ...
  return 0.0f;
}    

ChoiceIterator::ChoiceIterator(const LTRResultIterator& result_it) {
  ASSERT_HOST(result_it.it_->word() != NULL);
  ...
}

ChoiceIterator::~ChoiceIterator() {
  delete choice_it_;
}

bool ChoiceIterator::Next() {
  if (choice_it_ == NULL)
    return false;
  ...
}    

const char* ChoiceIterator::GetUTF8Text() const {
  if (choice_it_ == NULL)
    return NULL;
  ...
}    

float ChoiceIterator::Confidence() const {
  if (choice_it_ == NULL)
    return 0.0f;
  ...
  return confidence;
}    
}  // namespace tesseract.

我已经实现的代码:

//
using namespace tesseract;
//
char* concat(const char *s1, char *s2)
{
    char *result = (char*)malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}
//
#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */
//
JNIEXPORT jobjectArray JNICALL Java_com_mt_tesseract_ResultIterator_nativeGetChoices
  (JNIEnv *env, jclass clazz, jlong nativeResultIterator, jint level){

    PageIteratorLevel enumLevel = (PageIteratorLevel)level;
    ResultIterator *resultIterator = (ResultIterator *)nativeResultIterator;

    tesseract::ChoiceIterator ci(*resultIterator);
    int numberOfAlternatives = 0;
    do {
        numberOfAlternatives++;
    } while (ci.Next());

    jobjectArray ret = (jobjectArray)env->NewObjectArray(numberOfAlternatives, env->FindClass("java/lang/String"), env->NewStringUTF(""));

    int i = 0;
    tesseract::ChoiceIterator cb(*resultIterator);
    do {            
        const char *utfText = cb.GetUTF8Text();         
        char newString[5];
        sprintf(newString, "|%.2f", cb.Confidence());    
        char *result = concat(utfText, newString);    
        env->SetObjectArrayElement(ret, i, env->NewStringUTF(result));    
        free(result);           
        i++;
    } while (cb.Next());        
    return ret;
}

JNIEXPORT jstring JNICALL Java_com_mt_tesseract_ResultIterator_nativeGetUTF8Text
  (JNIEnv *env, jclass clazz, jlong nativeResultIterator, jint level){
    //
    ResultIterator *resultIterator = (ResultIterator *)nativeResultIterator;
    PageIteratorLevel enumLevel = (PageIteratorLevel)level;

    char *text = resultIterator->GetUTF8Text(enumLevel);
    jstring result = env->NewStringUTF(text);

    free(text);

    return result;
}

JNIEXPORT jfloat JNICALL Java_com_mt_tesseract_ResultIterator_nativeConfidence
  (JNIEnv *env, jclass clazz, jlong nativeResultIterator, jint level){
    //
    ResultIterator *resultIterator = (ResultIterator *)nativeResultIterator;
    PageIteratorLevel enumLevel = (PageIteratorLevel)level;

    return (jfloat)resultIterator->Confidence(enumLevel);
}    
//
#ifdef __cplusplus
}
#endif  /* __cplusplus */

MSVC 的结果:

Error   1   error LNK2001: unresolved external symbol "public: float __thiscall tesseract::ChoiceIterator::Confidence(void)const " (?Confidence@ChoiceIterator@tesseract@@QBEMXZ)   
Error   2   error LNK2001: unresolved external symbol "public: char const * __thiscall tesseract::ChoiceIterator::GetUTF8Text(void)const " (?GetUTF8Text@ChoiceIterator@tesseract@@QBEPBDXZ)    
Error   3   error LNK2001: unresolved external symbol "public: bool __thiscall tesseract::ChoiceIterator::Next(void)" (?Next@ChoiceIterator@tesseract@@QAE_NXZ) 
Error   4   error LNK2001: unresolved external symbol "public: __thiscall tesseract::ChoiceIterator::~ChoiceIterator(void)" (??1ChoiceIterator@tesseract@@QAE@XZ)   
Error   5   error LNK2001: unresolved external symbol "public: __thiscall tesseract::ChoiceIterator::ChoiceIterator(class tesseract::LTRResultIterator const &)" (??0ChoiceIterator@tesseract@@QAE@ABVLTRResultIterator@1@@Z)   
Error   6   error LNK1120: 5 unresolved externals
4

0 回答 0