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?