1

I have a class category where I can create various items and each item has its own container.

#ifndef CATEGORY_CPP
#define CATEGORY_CPP

#include <iostream>
#include <list>

class Category {
public:
    static std::list <std::string> Telephones;
    static std::list <std::string> Laptops;
public:
    Category(std::string newItem, std::string category) {
        if ( category == "Telephones" || category == "telephones") {
            Telephones.push_back(newItem);
        }
    
        if ( category == "Laptops" || category == "laptops") {
            Laptops.push_back(newItem);
        }
    }

instead of getters :

    static void showItems() {
        if ( Telephones.empty()) {
            std::cout << "Dont have any telephones!" << std::endl;
        } else {
        std::list<std::string>::iterator it;

        std::cout << "Telephones : " << std::endl;
        std::cout << "----------------" << std::endl;

        for ( it = Telephones.begin(); it != Telephones.end(); it++ ) {
            std::cout << *it << std::endl;
        }
        std::cout << "----------------" << std::endl;

        std::cout << std::endl;
        } 

        if ( Laptops.empty()) {
            std::cout << "Dont have any laptops!" << std::endl;
        } else {
        std::list<std::string>::iterator it;
        std::cout << "Laptops : " << std::endl;
        std::cout << "----------------" << std::endl;

        for ( it = Laptops.begin(); it != Laptops.end(); it++ ) {
            std::cout << *it << std::endl;
        }
        std::cout << "----------------" << std::endl;
        }
    }

for convenience, I make a method for creating an item :

    static void makeItem(std::string newItem, std::string category) {
        Category item(newItem, category);
    }
};

std::list <std::string> Category::Telephones;
std::list <std::string> Category::Laptops;


#endif //CATEGORY_CPP

and my main :

#include "Category.cpp"

int main() {
Category::makeItem("Iphone 11", "Telephones");
Category::makeItem("Iphone 12", "Telephones");
Category::makeItem("Iphone 13", "Telephones");
Category::makeItem("Samsung m21", "Telephones");
Category::makeItem("Msi345G", "Laptops");
Category::makeItem("Asus81XOL-BLACK", "Laptops");
Category::showItems();
}

I tried to make a header for Category, but it gives an identical error, so I think this is not the problem

my problem is this error :

C:\Users\Egor\AppData\Local\Temp\ccqIKw5E.o:main.cpp:(.bss+0x0): multiple definition of `Category::Telephones'
C:\Users\Egor\AppData\Local\Temp\ccCY2RRQ.o:Category.cpp:(.bss+0x0): first defined here
C:\Users\Egor\AppData\Local\Temp\ccqIKw5E.o:main.cpp:(.bss+0x8): multiple definition of `Category::Laptops'
C:\Users\Egor\AppData\Local\Temp\ccCY2RRQ.o:Category.cpp:(.bss+0x8): first defined here
collect2: ld returned 1 exit status

how to fix it?

smth
  • 9
  • 3
  • 5
    Never include a `.cpp` file. – drescherjm Dec 09 '21 at 15:54
  • 1
    `.cpp` files should be compiled into object files, and then those should be linked together to make a binary. You should never include a `.cpp` file. – NathanOliver Dec 09 '21 at 15:55
  • definition and declaration are two different thing that must be used carefully. Just organize your files in header + cpp and you will solve half of your future problems – Leos313 Dec 09 '21 at 15:58
  • and, also, try to clear the compilation folder before running another compilation. Just start from zero again – Leos313 Dec 09 '21 at 16:00
  • @NathanOliver changed but the error is the same – smth Dec 09 '21 at 16:24
  • `std::list Category::Telephones;` and `std::list Category::Laptops;` need to be in the cpp file and the `class Category { ... ];` needs to be in a header. – drescherjm Dec 09 '21 at 16:54

0 Answers0