首页 文章

正确编码和未命名的命名空间中的未解析函数

提问于
浏览
-1

几天来,我一直试图通过MSVS 15中的概念验证测试项目来解决几个问题 . 我看到的问题是命名空间中有一些无法解决的函数 .

自动搜索SO列表中的所有热门问题都没有帮助我,因为我的代码没有问题 . 谷歌也未能找到任何有用的东西;就像其他SO问题一样,这些问题与我的问题不同 .

据我所知,命名空间代码是正确的,但链接器仍然没有找到函数定义 . 命名空间中的所有函数都会收到未解析的链接器错误 .

util.h

#include <string>
#include <vector>
#include <map>
#include "logging.h"
#include <random>

using std::string;
using std::vector;
using std::map;
using std::pair;
using std::to_string;

namespace Util {
    //Utility Function Predefs
    map<string, float> fillMap(map<string, float>argMap);
    map<string, int> fillMap(map<string, int>argMap);

    void createBInfo(string errType, string fl, string lnNm, string dt, string tm, string errCode, string errMsg, string fPath);
    float calcPop(int pERand, int pSRand, int popRand1, int popRand2, int popRand3);
    int getDefenses(int rand, float eks);
    int getShields(int rand, float eks);
    int getIRand(int low, int high);
    float getFRand(float low, float high);
    int calcXP(int base, int level, float factor);

    //Utility Members
    extern map<string, float>::iterator itf1;
    extern map<string, int>::iterator iti1;
    extern map<string, float>rtnFMap;
    extern map<string, int>rtnIMap;

    extern int i1;
    extern int nOfDef;
    extern int nOfShd;

    //For Debug
    extern string file, bLocale, bTDate;
}

util.cpp

#include "util.h"
#include <math.h>
#include "consts.h"
#include "logging.h"

Logging l_u;
std::random_device rd;
std::mt19937 gen(rd());

namespace Util {
    //Name: fillMap
    //Description: Fill a map reference passed through with the contents of another map from dataSystem
    /*Arguments:
    *argMap1 - This map contains a map of settings loaded passed in from another function
    */
    map<string, float> fillMap(map<string, float>argMap1) {
        for (itf1 = argMap1.begin(); itf1 != argMap1.end(); itf1++) {
            rtnFMap.insert(pair<string, float>(itf1->first, itf1->second));
        }

        return rtnFMap;
    }

    //Name: fillMap
    //Description: Fill a map reference passed through with the contents of another map from dataSystem; int value override
    /*Arguments:
    *argMap1 - This map contains a map of settings loaded passed in from another function
    */
    map<string, int> fillMap(map<string, int>argMap1) {
        for (iti1 = argMap1.begin(); iti1 != argMap1.end(); iti1++) {
            rtnIMap.insert(pair<string, int>(iti1->first, iti1->second));
        }

        return rtnIMap;
    }


    void createBInfo(string errType, string fl, string lnNm, string dt, string tm, string errCode, string errMsg, string fPath) {
        file = fl;
        bLocale = "File: " + file + "  Line: " + lnNm;
        bTDate = dt + "  " + tm;

        l_u.createLReport(errType, errCode, errMsg, bLocale, bTDate, fPath);
    }

    int calcXP(int base, int level, float factor) {
        return trunc(base * (pow(level, factor)));
    }

    int getDefenses(int rand, float eks) {
        if ((eks > 0.0f) && (eks < 1.0f)) { //Type 0 Planet
            if ((rand >= 1) && (rand <= 35)) { nOfDef = 0; } //36% chance for 0 defenses        
            else if ((rand >= 36) && (rand <= 60)) { nOfDef = 1; } //25% chance for 1 defense
            else if ((rand >= 61) && (rand <= 78)) { nOfDef = 2; } //18% chance for 2 defenses
            else if ((rand >= 79) && (rand <= 87)) { nOfDef = 3; } //7% chance for 3 defenses
            else if ((rand >= 88) && (rand <= 94)) { nOfDef = 4; } //7% chance for 4 defenses
            else if ((rand >= 95) && (rand <= 98)) { nOfDef = 5; } //4% chance for 5 defenses
            else if (rand >= 99) { nOfDef = 6; } //2% Chance for 6 defenses
        }
        else if ((eks >= 1.0f) && (eks < 2.0f)) { //Type 1 Planet
            if ((rand >= 1) && (rand <= 28)) { nOfDef = 0; } //29% chance for 0 defenses        
            else if ((rand >= 29) && (rand <= 55)) { nOfDef = 1; } //28% chance for 1 defense
            else if ((rand >= 56) && (rand <= 71)) { nOfDef = 2; } //16% chance for 2 defenses
            else if ((rand >= 72) && (rand <= 82)) { nOfDef = 3; } //11% chance for 3 defenses
            else if ((rand >= 83) && (rand <= 90)) { nOfDef = 4; } //8% chance for 4 defenses
            else if ((rand >= 91) && (rand <= 96)) { nOfDef = 5; } //7% chance for 5 defenses
            else if (rand >= 97) { nOfDef = 6; } //4% Chance for 6 defenses
        }
        else if ((eks >= 2.0f) && (eks < 3.0f)) { //Type 2 Planet
            if ((rand >= 1) && (rand <= 28)) { nOfDef = 0; } //29% chance for 0 defenses        
            else if ((rand >= 29) && (rand <= 56)) { nOfDef = 1; } //28% chance for 1 defense
            else if ((rand >= 57) && (rand <= 70)) { nOfDef = 2; } //14% chance for 2 defenses
            else if ((rand >= 71) && (rand <= 82)) { nOfDef = 3; } //12% chance for 3 defenses
            else if ((rand >= 83) && (rand <= 91)) { nOfDef = 4; } //9% chance for 4 defenses
            else if ((rand >= 92) && (rand <= 96)) { nOfDef = 5; } //5% chance for 5 defenses
            else if (rand >= 97) { nOfDef = 6; } //4% Chance for 6 defenses
        }
        else if ((eks >= 3.0f) && (eks < 4.0f)) { //Type 3 Planet
            if ((rand >= 1) && (rand <= 20)) { nOfDef = 0; } //21% chance for 0 defenses        
            else if ((rand >= 21) && (rand <= 46)) { nOfDef = 1; } //26% chance for 1 defense
            else if ((rand >= 47) && (rand <= 68)) { nOfDef = 2; } //19% chance for 2 defenses
            else if ((rand >= 69) && (rand <= 80)) { nOfDef = 3; } //13% chance for 3 defenses
            else if ((rand >= 81) && (rand <= 89)) { nOfDef = 4; } //9% chance for 4 defenses
            else if ((rand >= 90) && (rand <= 95)) { nOfDef = 5; } //6% chance for 5 defenses
            else if (rand >= 96) { nOfDef = 6; } //3% Chance for 6 defenses
        }
        else if ((eks >= 4.0f) && (eks < 5.0f)) { //Type 4 Planet
            if (rand >= 1 && rand <= 18) { nOfDef = 0; } //19% chance for 0 defenses        
            else if (rand >= 19 && rand <= 35) { nOfDef = 1; } //15% chance for 1 defense
            else if (rand >= 36 && rand <= 55) { nOfDef = 2; } //20% chance for 2 defenses
            else if (rand >= 56 && rand <= 68) { nOfDef = 3; } //13% chance for 3 defenses
            else if (rand >= 69 && rand <= 80) { nOfDef = 4; } //12% chance for 4 defenses
            else if (rand >= 81 && rand <= 89) { nOfDef = 5; } //9% chance for 5 defenses
            else if (rand >= 90 && rand <= 95) { nOfDef = 6; } //6% Chance for 6 defenses
            else if (rand >= 96) { nOfDef = 7; } //5% chance for 7 defenses
        }
        else if (eks >= 5.0f) { //Type 5 Planet
            if (rand >= 1 && rand <= 12) { nOfDef = 0; } //13% chance for 0 defenses        
            else if (rand >= 13 && rand <= 28) { nOfDef = 1; } //16% chance for 1 defense
            else if (rand >= 29 && rand <= 48) { nOfDef = 2; } //20% chance for 2 defenses
            else if (rand >= 49 && rand <= 62) { nOfDef = 3; } //14% chance for 3 defenses
            else if (rand >= 63 && rand <= 74) { nOfDef = 4; } //12% chance for 4 defenses
            else if (rand >= 75 && rand <= 85) { nOfDef = 5; } //11% chance for 5 defenses
            else if (rand >= 86 && rand <= 92) { nOfDef = 6; } //9% Chance for 6 defenses
            else if (rand >= 93 && rand <= 97) { nOfDef = 7; } //5% Chance for 7 defenses
            else if (rand >= 98) { nOfDef = 8; } //3% chance for 8 defenses
        }

        return nOfDef;
    }

    int getShields(int rand, float eks) {
        if ((eks > 0.0f) && (eks < 1.0f)) { //Type 0 Planet
            if ((rand >= 1) && (rand <= 60)) { nOfShd = 0; } //61% chance for 0 shields
            else if ((rand >= 61) && (rand <= 86)) { nOfShd = 1; } //26% chance for 1 shields
            else if (rand >= 86) { nOfShd = 2; } //17% chance for 2 shields
        }
        else if ((eks >= 1.0f) && (eks < 2.0f)) { //Type 1 Planet
            if ((rand >= 1) && (rand <= 52)) { nOfShd = 0; } //53% chance for 0 shields
            else if ((rand >= 53) && (rand <= 84)) { nOfShd = 1; } //22% chance for 1 shields
            else if (rand >= 85) { nOfShd = 2; } //15% chance for 2 shields
        }
        else if ((eks >= 2.0f) && (eks < 3.0f)) { //Type 2 Planet
            if ((rand >= 1) && (rand <= 35)) { nOfShd = 0; } //36% chance for 0 shields
            else if ((rand >= 36) && (rand <= 68)) { nOfShd = 1; } //33% chance for 1 shields
            else if ((rand >= 68) && (rand <= 87)) { nOfShd = 2; } //18% chance for 2 shields
            else if (rand >= 88) { nOfShd = 3; } //13% chance for 3 shields
        }
        else if ((eks >= 3.0f) && (eks < 4.0f)) { //Type 3 Planet
            if ((rand >= 1) && (rand <= 27)) { nOfShd = 0; } //28% chance for 0 shields
            else if ((rand >= 28) && (rand <= 64)) { nOfShd = 1; } //27% chance for 1 shields
            else if ((rand >= 65) && (rand <= 84)) { nOfShd = 2; } //21% chance for 2 shields
            else if (rand >= 85) { nOfShd = 3; } //16% chance for 3 shields
        }
        else if ((eks >= 4.0f) && (eks < 5.0f)) { //Type 4 Planet
            if ((rand >= 1) && (rand <= 20)) { nOfShd = 0; } //21% chance for 0 shields
            else if ((rand >= 21) && (rand <= 48)) { nOfShd = 1; } //28% chance for 1 shields
            else if ((rand >= 49) && (rand <= 74)) { nOfShd = 2; } //26% chance for 2 shields
            else if ((rand >= 75) && (rand <= 88)) { nOfShd = 3; } //14% chance for 3 shields
            else if (rand >= 89) { nOfShd = 4; } //12% chance for 4 shields
        }
        else if (eks >= 5.0f) { //Type 5 Planet
            if ((rand >= 1) && (rand <= 18)) { nOfShd = 0; } //19% chance for 0 shields
            else if ((rand >= 19) && (rand <= 36)) { nOfShd = 1; } //18% chance for 1 shields
            else if ((rand >= 37) && (rand <= 65)) { nOfShd = 2; } //29% chance for 2 shields
            else if ((rand >= 66) && (rand <= 84)) { nOfShd = 3; } //19% chance for 3 shields
            else if (rand >= 85) { nOfShd = 4; } //16% chance for 4 shields
        }

        return nOfShd;
    }

    float calcPop(int pERand, int pSRand, int popRand1, int popRand2, int popRand3) {
        return ((((((8000 * popRand1) * popRand2) * pERand) * pSRand) * popRand3) / 6);
    }

    int getIRand(int low, int high) {
        return std::uniform_int_distribution<>{low, high}(gen);
    }

    float getFRand(float low, float high) {
        return std::uniform_real_distribution<float> {low, high}(gen);
    }
}

引用错误的命名空间函数调用的摘录示例:

#include <iostream>
#include <stdlib.h>
#include "planet.h"
#include <Windows.h>
#include <sstream>
#include <cmath>
#include "util.h"
#include "consts.h"

...

void Planet::createBelts() {
    beltRand = Util::getIRand(0, 10);

    if (beltRand != 0) {
        for (i = 1; i <= beltRand; i++) {
            ramount = Util::getIRand(3, 15); //External value tag: int range
            size = ((ramount * Util::getFRand(1000.0f, 50000.00f) * 46) / 2); //External value tag: float range
            name = plName + " Asteroid Belt " + rNumerals[i - 1];

            addBelt(name, size, ramount, false);
        }
    }
}

...

更新:

以下是我看到的一些链接器错误 .

未解析的外部符号“float __cdecl Util :: calcPop(int,int,int,int,int)”(?calcPop @ Util @@ YAMHHHHH @ Z)在函数“public:void __thiscall planetSystem :: generatePlanets(void)”中引用(?generatePlanets @ planetSystem @@ QAEXXZ)未解析的外部符号“float __cdecl Util :: getFRand(float,float)”(?getFRand @ Util @@ YAMMM @ Z)未解析的外部符号“float __cdecl Util :: getFRand(float,float )“(?getFRand @ Util @@ YAMMM @ Z)未解析的外部符号”float __cdecl Util :: getFRand(float,float)“(?getFRand @ Util @@ YAMMM @ Z)未解析的外部符号”int __cdecl Util :: getDefenses (int,float)“(?getDefenses @ Util @@ YAHHM @ Z)

1 回答

  • 0

    所以,经过四天我终于发现了与命名空间问题有关的问题 . 项目所在的解决方案文件是在MSVS 2012中生成的,并且在不久前转换为MSVS 2015 . 我不知道为什么这很重要,但是当我在2015年创建一个全新的项目并将代码文件放入其中时,我的命名空间未解决的链接器错误得到修复 .

相关问题