C++ 错误 LNK2005 和错误 LNK1169
c++ error LNK2005 and error LNK1169
我开始在我的电脑上构建一个项目。该项目在我的计算机上编译,但是当我将它复制到另一台计算机时出现致命错误(它在 visual c++ express 2010 上工作)。它仍然很小,所以我将复制所有项目。
源文件->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);
}
源文件->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);
}
头文件->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
这是它给我的错误:
error LNK2005: "class Products * nodePtr" (?nodePtr@@3PAVProducts@@A) 已经在 List.obj
中定义
错误 LNK2005:"class Products * first" (?first@@3PAVProducts@@A) 已在 List.obj
中定义
错误 LNK1169:找到一个或多个多次定义的符号
如果你必须使用全局变量(这通常是个坏主意),那么你不能在 header 中定义它们。它们受单一定义规则的约束,因此只能在一个源文件中有一个定义。
在 header 中声明它们:
extern Products* first;
并在源文件中定义它们:
Products* first;
但听起来您想要的东西更像是注释掉的声明:指向第一个 node
的指针,作为 Products
class 的成员,没有奇怪的全局变量。
我开始在我的电脑上构建一个项目。该项目在我的计算机上编译,但是当我将它复制到另一台计算机时出现致命错误(它在 visual c++ express 2010 上工作)。它仍然很小,所以我将复制所有项目。
源文件->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);
}
源文件->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);
}
头文件->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
这是它给我的错误:
error LNK2005: "class Products * nodePtr" (?nodePtr@@3PAVProducts@@A) 已经在 List.obj
中定义
错误 LNK2005:"class Products * first" (?first@@3PAVProducts@@A) 已在 List.obj
中定义
错误 LNK1169:找到一个或多个多次定义的符号
如果你必须使用全局变量(这通常是个坏主意),那么你不能在 header 中定义它们。它们受单一定义规则的约束,因此只能在一个源文件中有一个定义。
在 header 中声明它们:
extern Products* first;
并在源文件中定义它们:
Products* first;
但听起来您想要的东西更像是注释掉的声明:指向第一个 node
的指针,作为 Products
class 的成员,没有奇怪的全局变量。