C ++中未解析的外部符号(使用VS2017)

时间:2018-04-25 11:37:47

标签: c++ class static std precision

当我运行此代码时,在VS2017中我收到了这些错误

  

LNK2001未解析的外部符号“public:static class std :: basic_string,class std :: allocator> UberDriver :: name”(?name @ UberDriver @@ 2V?$ basic_string @ DU?$ char_traits @ D @ std @ @V?$ allocator @ D @ 2 @@ std @@ A)ConsoleApplication4 C:\ Users \ pavlkara \ source \ repos \ OOP ex2 \ ConsoleApplication4 \ ConsoleApplication4.obj 1

     

LNK2001未解析的外部符号“public:static int UberDriver :: fee_per_km”(?fee_per_km @ UberDriver @@ 2HA)ConsoleApplication4 C:\ Users \ pavlkara \ source \ repos \ OOP ex2 \ ConsoleApplication4 \ ConsoleApplication4.obj 1

     

LNK2001未解析的外部符号“public:static double UberDriver :: uber_wallet”(?uber_wallet @ UberDriver @@ 2NA)ConsoleApplication4 C:\ Users \ pavlkara \ source \ repos \ OOP ex2 \ ConsoleApplication4 \ ConsoleApplication4.obj 1

     

LNK2001未解析的外部符号“public:static double UberDriver :: uber_points”(?uber_points @ UberDriver @@ 2NA)ConsoleApplication4 C:\ Users \ pavlkara \ source \ repos \ OOP ex2 \ ConsoleApplication4 \ ConsoleApplication4.obj 1

     

LNK2001未解析的外部符号“public:static int UberDriver :: customer_counter”(?customer_counter @ UberDriver @@ 2HA)ConsoleApplication4 C:\ Users \ pavlkara \ source \ repos \ OOP ex2 \ ConsoleApplication4 \ ConsoleApplication4.obj 1

我已阅读有关LNK2001错误的文档,但无法解决问题。你能来看看吗?

#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <locale>
#include <string>
#include <string.h>
#include <iomanip>

class UberDriver {
public:
    static std::string name;
    static int fee_per_km;
    static double uber_wallet;
    static double uber_points;
    static int customer_counter;
    UberDriver(std::string n, int fpk, double uw, double up, int cc) {
        name = n;
        fee_per_km = fpk;
        uber_wallet = uw;
        uber_points = up;
        customer_counter = cc;
        std::cout << "Your profile is created" << std::endl;
    };
    std::string get_name() { return name; }
    int get_fee_per_km() { return fee_per_km; }
    double get_uber_wallet() { return uber_wallet; }
    double get_uber_points() { return uber_points; }
    int get_customer_counter() { return customer_counter; }
    void set_name(std::string n) {
        name = n;
    }
    void set_fee_per_km(int fpk) {
        fee_per_km = fpk;
    }
    void set_uber_wallet(double uw) {
        uber_wallet = uw;
    }
    void set_uber_points(double up) {
        uber_points = up;
    }
    void set_customer_counter(int cc) {
        customer_counter = cc;
    }
    void print_uber_profile() {
        double avg;
        avg = (uber_points / customer_counter);
        std::cout << "Name: " << name << "- Fee: " << fee_per_km << "- Rating: ";
        std::cout << std::fixed << std::setprecision(2) << avg << std::endl;
    }
    ~UberDriver() {
        if (uber_wallet >= 100.00) {
            std::cout << "Great Job" << std::endl;
        }
        else if (uber_wallet < 100.00&&uber_wallet >= 50.00) {
            std::cout << "Good Effort" << std::endl;
        }
        else {
            std::cout << "Try Harder" << std::endl;
        }
    }
};

void uber_call(UberDriver driver, double km) {
    double l;
    int customer_points = 0;
    if (driver.uber_points > 6 && driver.uber_points < 9) {
        customer_points = 8;
        l = km * (driver.fee_per_km);
    }
    else if (driver.uber_points <= 6) {
        customer_points = 4;
        driver.customer_counter = driver.fee_per_km / 2;
        l = km * (driver.fee_per_km);
    }
    else if (driver.uber_points >= 9) {
        l = km * (driver.fee_per_km) + 5.0;
        customer_points = 10;
    }
    std::cout << "Customer Number: " << driver.customer_counter + 1 << " Cost of the fare:";
    std::cout << std::fixed << std::setprecision(2) << l << std::endl;
    std::cout << " Customer Points: " << customer_points;
};

int main() {
    UberDriver driver("Arya Stark", 12, 101.5, 4.7, 23);
    std::cout << driver.name;
    driver.customer_counter = 21;
    driver.uber_points = 9.6;
    uber_call(driver, 15.0);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您已声明静态变量,这些变量未定义。从班级中的所有成员中删除static关键字。那不是,static意味着什么。