问题描述:

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" ([email protected]@[email protected]@A) already defined in List.obj

error LNK2005: "class Products * first" ([email protected]@[email protected]@A) already defined in List.obj

error LNK1169: one or more multiply defined symbols found

网友答案:

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.

相关阅读:
Top