繁体   English   中英

C++ Linker 错误 function 已定义

[英]C++ Linker Errors mulitple function defined

在尝试编译程序时一直遇到定义的多个函数和 linker 错误,不太确定我什至如何在 header 中正确定义所有内容。

该程序的目的是检查 main.cpp/test 文件中两个条目的有效性。 我制作了一个程序来检查测试文件中的条目。

CarInventory.cpp文件

       #include <iostream>
       #include <string>
       #include <iomanip>
       #include <string.h>
       #include "CarInventory.h"
       using namespace std;
       using namespace sdds;
       void CarInventory::resetInfo()

      {

         m_type = nullptr;

         m_brand = nullptr;

         m_model = nullptr;

         m_year = 0;

         m_code = 0;

    m_price = 0;

}

CarInventory::CarInventory()

{

    resetInfo();

}

CarInventory::CarInventory(const char* type, const char* brand, const char* model, int year, int code, double price)

{

    resetInfo();

    setInfo(type, brand, model, year, code, price);

}

CarInventory::CarInventory(const char* type, const char* brand, const char* model)

{

    resetInfo();

    setInfo(type, brand, model, 2022, 100, 1);

}

CarInventory::~CarInventory()

{

    delete[] m_type;

    delete[] m_brand;

    delete[] m_model;

}

CarInventory& CarInventory::setInfo(const char* type, const char* brand, const char* model, int year, int code, double price)

{

    if (type == nullptr || brand == nullptr || model == nullptr || year < 1990 || code < 100 || price < 0)

    {

        resetInfo();

    }

    else

    {

        delete[] m_type;

        delete[] m_brand;

        delete[] m_model;

        m_type = new char[strlen(type) + 1];

        strcpy_s(m_type, sizeof(type), type);

        m_brand = new char[strlen(brand) + 1];

        strcpy_s(m_brand, sizeof(brand),  brand);

        m_model = new char[strlen(model) + 1];

        strcpy_s(m_model, sizeof(m_model),  model);

        m_year = year;

        m_code = code;

        m_price = price;

    }

    return *this;

}

bool CarInventory::isValid() const

{

    return m_type != nullptr && m_brand != nullptr && m_model != nullptr && m_year >= 1990 && m_code >= 100 && m_price >= 0;

}

void CarInventory::printInfo() const

{

    std::cout << "|" << std::setw(12) << m_type << "|" << std::setw(18) << m_brand << "|" << std::setw(18) << m_model << "|" << std::setw(6) << m_year << "|" << std::setw(6) << m_code << "|" << std::setw(11) << m_price << "|" << std::endl;

}

bool CarInventory::isSimilarTo(const CarInventory& car) const

{

    return m_type == car.m_type && m_brand == car.m_brand && m_model == car.m_model && m_year == car.m_year;

}

namespace sdds {

    bool find_similar(CarInventory car[], const int num_cars)

    {

        for (int i = 0; i < num_cars; i++)

        {

            for (int j = i + 1; j < num_cars; j++)

            {

                if (car[i].isSimilarTo(car[j]))

                {

                    return true;

                }

            }

        }

        return false;

    }
}

Header 文件

#ifndef CARINVENTORY_H
#define CARINVENTORY_H
#define _CRT_SECURE_NO_WARNINGS

namespace sdds {
class CarInventory {
    char* m_type;
    char* m_brand;
    char* m_model;
    int m_year;
    int m_code;
    double m_price;
    void resetInfo();
public:
    CarInventory();
    CarInventory(const char* type, const char* brand, const char* model, int year, int code, double price);
    CarInventory(const char* type, const char* brand, const char* model);
    bool isSimilarTo(const CarInventory& car) const;
    ~CarInventory();
    CarInventory& setInfo(const char* type, const char* brand, const char* model, int year, int code, double price);
    void printInfo() const;
    bool isValid() const;
    bool find_similar(CarInventory car[], const int num_cars);

  };
 }
    #endif

测试/主文件

#include<iostream>
#include<iomanip>
#include "CarInventory.h"
#include "CarInventory.cpp"
using namespace std;
using namespace sdds;

 int main()
 {
const int num_cars = 6;
bool invalid_data = false;
CarInventory cars[num_cars] = {
    {},
    {"suv", "volvo", "xc90"},
    {"Truck", "Ford", "F 150", 2021, 105, 55000},
    {"Sedan", "BMW", "M550i", 2022, 101, 91000},
    {"Truck", "Tesla", "Cybertruck", 2021, 102, 65000},
    {"Sedan", "BMW", "M550i"}
};

if (cars[2].setInfo("SUV", "Volvo", "XC90", 2019, 109, 80000).isValid()) {
    cout << "Information was set correctly!" << endl;
}
else {
    cout << "Information was set incorrectly!" << endl;
}
if (cars[1].setInfo("SUV", "Volvo", "XC90", 1234, 1, 1).isValid()) {
    cout << "Information was set correctly!" << endl;
}
else {
    cout << "Information was set incorrectly!" << endl;
}


cout << setw(60) << "----- Car Inventory Information -----" << endl << endl;;
cout << "| Type       | Brand            | Model            | Year | Code |     Price |" << endl;
cout << "+------------+------------------+------------------+------+------+-----------+" << endl;
for (int i = 0; i < num_cars; i++) {
    if (cars[i].isValid())
        cars[i].printInfo();
    else
        invalid_data = true;
}

if (invalid_data) {
    cout << endl;
    cout << setfill('*') << setw(60) << "*" << endl;
    cout << "*  WARNING: There are invalid data in the inventory!      *" << endl;
    cout << setfill('*') << setw(60) << "*" << endl;
}
if (find_similar(cars, num_cars)) {
    cout << endl;
    cout << setfill('+') << setw(60) << "+" << endl;
    cout << "+  WARNING: There are similar entries in the inventory!   +" << endl;
    cout << setfill('+') << setw(60) << "+" << endl;
}
return 0;
  }

我遇到的编译器错误

图片

您需要从main.cpp文件中删除#include "CarInventory.cpp" 它不属于那里。

您的项目已经将CarInventory.cpp编译成CarInventory.obj ,然后将该.obj链接到最终的.exe 但是您要求编译器将CarInventory.cpp的所有代码包含到main.cpp的代码中,该代码被编译到main.obj中。 您收到有关在两个.obj文件中重复的所有CarInventory方法的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM