c++ - 链接器错误 2001 未解析的外部符号

标签 c++ vector stl linker linker-errors

我以前从未遇到过这样的错误,请记住,我还在上学,所以我做这件事的时间不长。

我得到的错误是:

Error 1 error LNK2001: unresolved external symbol "private: static class std::vector > Job::names" (?names@Job@@0V?$vector@PBDV?$allocator@PBD@std@@@std@@A) Job.obj PayrollCalculator

Error 2 error LNK2001: unresolved external symbol "private: static class std::vector > Job::percentBased" (?percentBased@Job@@0V?$vector@_NV?$allocator@_N@std@@@std@@A) Job.obj PayrollCalculator

Error 3 error LNK2001: unresolved external symbol "private: static class std::vector > Job::pay" (?pay@Job@@0V?$vector@MV?$allocator@M@std@@@std@@A) Job.obj PayrollCalculator

我不知道从哪里开始出现这样的错误,所以如果有人可以提供帮助。 如果有帮助,这里是相关代码:

stdafx.h:

#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <limits>
#include <vector>

工作.h:

#include "stdafx.h"
class Job
{
public:
    Job();
    Job(const char*);

    const char* getName();
    float getPay();
private:
    static std::vector<const char*> names;
    static std::vector<bool> percentBased;
    static std::vector<float> pay;
    short jobType = -1;

    void defineNew(const char*);
    void setPay();
};

作业.cpp

#include "stdafx.h"
#include "Job.h"

Job::Job()
{
    // Predefined jobs
    const char* jobs[] = {/*Enter names of jobs here*/ "agent", "lawyer", "pa", "trainer" };
    bool percentPays[] = {/*Enter weather jobs base pay off a percentage of the employers income here*/ true, true, true, true };
    float pays[] = {/*Enter the percentage or base pay for each job here*/ (float).07, (float).1, (float).03, (float).05 };

    // Initilize Vectors
    names.assign(jobs, jobs + sizeof(jobs));
    percentBased.assign(percentPays, percentPays + sizeof(percentPays));
    pay.assign(pays, pays + sizeof(pays));
}

Job::Job(const char* jobName)
{
    // Test to see if the inputed job type is alrady defined
    for (unsigned int i = 0; i <= names.size(); i++)
    {
        if (jobName == names[i])
        {
            jobType = i;
            break;
        }
    }

    // Define new job
    if (jobType < 0)
    {
        defineNew(jobName);
        jobType = names.size() + 1;
    }
}

void Job::defineNew(const char* newName)
{
    // Add job to list of jobs
    names.push_back(newName);

    // Determine if job pay is based of percentage of employers income
    std::cout << "The entered job title is not predefined.\n"
              << "Dose the job " << newName << " have a pay based on a percentage of the employer's Income? [y/n] ";
    for (;;)
    {
        // Get input
        std::string input;
        std::getline(std::cin, input);
        // Convert input into useable data
        for (std::string::iterator it = input.begin(); it != input.end(); it++)
        {
            if (isalpha(*it) && isupper(*it)) *it = tolower(*it);
        }

        // Test input if yes job is percent based pay other wise no
        if (input == "yes" || input == "y")
        {
            percentBased.push_back(true);
            break;
        }
        else if (input == "no" || input == "n")
        {
            percentBased.push_back(false);
            break;
        }
        else
        {
            std::cout << "Invalid Input! Enter only yes or no.\n"
                      << "Dose the job " << newName << " have a pay based on a percentage of the employer's income? [y/n] ";
        }
    }

    setPay();
}

void Job::setPay()
{
    // Set new pay
    if (percentBased.back())
    {
        std::cout << "Enter the percentage of the employer's income the pay is based on: ";
    }
    else
    {
        std::cout << "Enter the jobs base pay: ";
    }

    for (;;)
    {
        // Get input
        std::string input;
        std::getline(std::cin, input);
        // Convert input to useable data
        bool goodInput = true, decimal = false;
        for (std::string::iterator it = input.begin(); it != input.end(); it++)
        {
            if (ispunct(*it) && !decimal)
            {
                decimal = true;
            }
            else if (!isalnum(*it))
            {
                goodInput = false;
            }
            else
            {
                std::cout << "Invalid Input! Input only numbers.\n"
                          << "Enter a number greater than 0: ";
            }
        }

        // Add pay information if good
        float tempPay = std::stof(input);
        if (goodInput && percentBased.back() && (tempPay >= 0 && tempPay < 1))
        {
            pay.push_back(tempPay);
            break;
        }
        else if (goodInput && percentBased.back() && !(tempPay >= 0 && tempPay < 1))
       {
            std::cout << "Invalid Input! Input only numbers between 0 and 1.\n"
                      << "Enter percentage greater than 0 and less than 1: ";
        }
        else if (goodInput && !percentBased.back() && tempPay >= 0)
        {
            pay.push_back(tempPay);
            break;
        }
        else if (goodInput && !percentBased.back() && tempPay < 0)
        {
            std::cout << "Invalid Imput! Input only numbers greater than 0.\n"
                      << "Enter a base pay greater than 0: ";
        }
    }
}

const char* Job::getName()
{
    return names[jobType];
}

float Job::getPay()
{
    return pay[jobType];
}

如果重要的话,我也在使用 Visual Studios 2013。

最佳答案

您只在 Job.h 文件中声明了 names, percentBased, pay,您需要在 Job.cpp 中定义这些静态变量

std::vector<const char*> Job::names;
std::vector<bool> Job::percentBased;
std::vector<float> Job::pay;

Job::Job()
{
//.....

关于c++ - 链接器错误 2001 未解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027745/

相关文章:

c++ - 推荐用于大数据处理的 C++ 并行化库是什么

javascript - SVG 不在 Firefox 中呈现字体(适用于 IE9 和 Chrome)

c++ - 如何遍历存储在 map 中的对象 vector ? C++

c++ - 列表迭代器不可取消引用

c++ - 哪种数据结构和算法适用于此?

c++ - Fortran 77 转换为 C++

c++ - Clang 或 GCC 是否能够自动矢量化手动展开的循环?

c++ - STL 中的迭代器指针如何工作

c++ - 为什么我的析构函数似乎比构造函数更频繁地被调用?

python - 如何使用 Numpy 将 (1*3) 向量除以 (3*3) 矩阵? a/b 不起作用