2016-01-23 63 views
-1

我是一个visual studio 2015 C++ newby谁试图在家里写一些游戏代码。visual studio 2015 C++无法解析的外部符号链接错误

我得到这个链接错误: LNK2019解析的外部符号 “公用:类的std :: basic_string的,一流的std ::分配器> __thiscall display_utils :: fit_int_2(INT)”(fit_int_2 @ display_utils @@ QAE? AV:$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ H @ Z)在函数“public:void __thiscall bat_stats :: disp_bat_stats(struct bat_stats :: bat_stats_typ )“(?disp_bat_stats @ bat_stats @@ QAEXUbat_stats_typ @ 1 @@ Z)

它显然不喜欢我用来从函数fit_int_2访问返回的字符串的字符串。我谷歌搜索的解决方案,但找不到任何解决我的问题。请注意,在我添加fit_int_2调用之前编译和链接的代码。如果你能帮助我,请提前致谢。该代码是下面:

bat_stats.h
#pragma once 

class bat_stats 
{ 
public: 
    struct bat_stats_typ 
    { 
     int gm; 
     int ab; 
     int ht; 
     int dbl; 
     int trpl; 
     int hr; 
     int rbi; 
     int sb; 
     int cs; 
     int bb; 
     int ibb; 
     int sf; 
     int sac; 
     int k; 
     int gidp; 
     int err; 
     float ave; 
     float slg; 
     float obp; 
    }; 
    void disp_bat_hdr(); 
    void disp_bat_stats(bat_stats_typ); 
private: 
    int dummy; 
}; 

bat_stats.cpp
#include <iostream> 
using std::cout; 
std::cin; 

#include <string> 
using std::string; 

#include "bat_stats.h" 
#include "display_utils.h" 

void bat_stats::disp_bat_hdr() 
{ 
    cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n"; 
} 

void bat_stats::disp_bat_stats(bat_stats_typ bat) 
{ 
    display_utils dut; 

    string s; 

    s = dut.fit_int_2(bat.gm);   // <- the problem is here! 
    cout << s << bat.gm << " "; 
    cout << bat.ab << "\n\n"; 
} 

display_utils.h
#pragma once 

#include <string> 
using std::string; 

class display_utils 
{ 
public: 
    void insert_5_lines(); 
    string fit_int_2(int); 
private: 
    int dummy; 
}; 

display_utils.cpp
#include <iostream> 
using std::cout; 

#include "display_utils.h" 

void display_utils::insert_5_lines() 
{ 
    cout << "\n\n\n\n\n"; 
} 

string fit_int_2(int i0) 
{ 
    string s0 = ""; 

    if (i0 < 10) 
    { 
     s0 = " "; 
    } 
    return s0; 
} 
+2

你需要检查你的项目实际上包含'display_utils.cpp'。编写.cpp文件不会执行任何操作 - 您必须将其添加到Visual Studio项目中。 – PaulMcKenzie

回答

0

您需要更改

string fit_int_2(int i0) 

string display_utils::fit_int_2(int i0) 

(您需要定义的成员函数 - 目前您定义无关全局函数)

+0

就是这样!非常感谢。我本该看到这个,但错过了。再次感谢。 – swimn4ever

相关问题