0
votes

I started building a project on my computer. the project compiled on my computer, but when i copied it to another computer it had fatal errors(its work on visual c++ express 2010). its still small so i will just copy all the project.

source file->main.cpp:

#include <iostream>
#include <string>
using namespace std;
#include "List.h"
void products_menu(){
    return;
}
void stores_menu(){
    return;
}
void costumers_menu(){
    return;
}
int main(){
    int option;
    Products a;
    do{
        cin>>option;
        if(option==1)
            products_menu();
            //option funcion
        if(option==2)
            stores_menu();
            //option funcion
        if(option==3)
            costumers_menu();
            //option funcion
    }while(option!=4);
}

source file->List.cpp:

#include <iostream>
#include <string>
using namespace std;
#include "List.h"
void products_menu(){
    return;
}
void stores_menu(){
    return;
}
void costumers_menu(){
    return;
}
int main(){
    int option;
    Products a;
    do{
        cin>>option;
        if(option==1)
            products_menu();
            //option funcion
        if(option==2)
            stores_menu();
            //option funcion
        if(option==3)
            costumers_menu();
            //option funcion
    }while(option!=4);
}

Header files->List.h:

#pragma once
#ifndef LIST_H
#define LIST_H
#include <string>
using namespace std;

class Products{
    private:
        typedef struct node{
            int id;
            string name;
            int price;
            node* next;
        };
        //typedef struct node* nodePtr;
        //nodePtr head;

    public:
        Products();
        //~Products();
        void addProduct(int id, string& name, int price);
        void updateNameProduct(int id, string& oldName, string& newName);
        void updatePriceProduct(int id, int oldPrice, int newPrice);
        void printProducts();//
    };
Products* first;
Products* nodePtr;
#endif

and this is the errors it gives me:

error LNK2005: "class Products * nodePtr" (?nodePtr@@3PAVProducts@@A) already defined in List.obj
error LNK2005: "class Products * first" (?first@@3PAVProducts@@A) already defined in List.obj
error LNK1169: one or more multiply defined symbols found

1
i found out that its work when i take the Products* first; Products* nodePtr; lines. but why doesnt it work with them?bhh bu
Declare Products* first; and Products* nodePtr; as extern in the header, and once in List.cpp.πάντα ῥεῖ
Just a thing to think about: You have tree functions that return nothing, but each has a return statement as its only content. Then, you have a function returning an integer but without any return statement. Then, check out stackoverflow.com/questions/8646421/…Ulrich Eckhardt
int main() {...} But you return nothing.Morb

1 Answers

0
votes

If you must use global variables (which is usually a bad idea), then you can't define them in a header. They're subject to the One Definition Rule, so can only have a definition in one source file.

Declare them in the header:

extern Products* first;

and define them in a source file:

Products* first;

But it sounds like you want something more like the commented out declarations: a pointer to the first node, as a member of the Products class, with no strange global variables.