简体   繁体   English

C++ Linker 错误 function 已定义

[英]C++ Linker Errors mulitple function defined

Have been stuck with multiple functions defined and linker errors while trying to compile the program, not too sure how I even defined everything properly in the header.在尝试编译程序时一直遇到定义的多个函数和 linker 错误,不太确定我什至如何在 header 中正确定义所有内容。

The purpose of the program is to check the validity of two entries from the main.cpp/test file.该程序的目的是检查 main.cpp/test 文件中两个条目的有效性。 I made a program which checks the entries from the test file.我制作了一个程序来检查测试文件中的条目。

CarInventory.cpp 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 File 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

Test/main file测试/主文件

#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;
  }

Compiler Error I've been getting我遇到的编译器错误

图片

You need to remove #include "CarInventory.cpp" from your main.cpp file.您需要从main.cpp文件中删除#include "CarInventory.cpp" It does not belong there.它不属于那里。

Your project is already compiling CarInventory.cpp into CarInventory.obj and then linking that .obj into the final .exe .您的项目已经将CarInventory.cpp编译成CarInventory.obj ,然后将该.obj链接到最终的.exe But you are asking the compiler to include all of CarInventory.cpp 's code into main.cpp 's code, which is compiled into main.obj .但是您要求编译器将CarInventory.cpp的所有代码包含到main.cpp的代码中,该代码被编译到main.obj中。 You are getting errors about all of CarInventory 's methods that are being duplicated in both .obj files.您收到有关在两个.obj文件中重复的所有CarInventory方法的错误。

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

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