c++ error LNK2005 and error LNK1169 - c++

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

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.

Related

LNK2005 function already defined in

I read that "if you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.". But in my code all the functions that I get errors on are in a .cpp file.
List.h
#pragma once
typedef struct list
{
int lin;
int col;
int val;
struct list* next;
struct list* prev;
}list;
List.cpp
#include "List.h"
bool empty(list*& start)
{
return (start == nullptr);
}
Matrice.h
#pragma once
#include "List.h"
class Matrice
{
private:
list* start;
list* finish;
public:
Matrice() :start(nullptr), finish(nullptr) {}
Matrice(const Matrice& matrice);
};
Matrice.cpp
#include "Matrice.h"
#include "List.cpp"
Matrice::Matrice(const Matrice& matrice)
{
if (empty(start))
{
// Code
}
}
Source.cpp
#include "Matrice.h"
#include <iostream>
int main()
{
Matrice a;
Matrice b;
a = b;
}
I added all the files, maybe there's something I don't see. The error is on the bool empty(list*& start) function ("already defined in List.obj").
You have an #include<List.cpp> in your Matrice.cpp and as you compile and link all cpp files together this will result duplicate definitions of everything defined in List.cpp as they are also defined in Matrice.cpp due to the include.
Replace the #include<List.cpp> with #include<List.h> and add the declaration of empty into the List.h

Getting class type redefinition and a few other errors

I'm creating a student data management console application for a project. I created a class called Student which is storing all the data that a student needs to have, and it also has all the getters and setters associated with it. Here is how all my files are laid out:
Student.h
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string id;
string email;
int presentation;
int essay1;
int essay2;
int project;
public:
//constructor
//Student();
//setters
void set_name(string);
void set_id(string);
void set_email(string);
void set_presentation(int);
void set_essay1(int);
void set_essay2(int);
void set_project(int);
//getters
string get_name();
string get_id();
string get_email();
int get_presentation();
int get_essay1();
int get_essay2();
int get_project();
};
Student.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
//constructor definition
/*
Student::Student(void) {
cout << "Student created" << endl;
}
*/
//setter definition
void Student::set_name(string s) {
name = s;
}
void Student::set_id(string s) {
id = s;
}
void Student::set_email(string s) {
email = s;
}
void Student::set_presentation(int a) {
presentation = a;
}
void Student::set_essay1(int a) {
essay1 = a;
}
void Student::set_essay2(int a) {
essay2 = a;
}
void Student::set_project(int a) {
project = a;
}
//getter definition
string Student::get_name() {
return name;
}
string Student::get_id() {
return id;
}
string Student::get_email() {
return email;
}
int Student::get_presentation() {
return presentation;
}
int Student::get_essay1() {
return essay1;
}
int Student::get_essay2() {
return essay2;
}
int Student::get_project() {
return project;
}
Main.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main() {
cout << "Hello World!" << endl;
Student student1;
Student student2;
Student student3;
student1.set_name("John");
student2.set_name("Bob");
student3.set_name("Carl");
return 0;
}
When I try to run my program, I get, amongst others, the following errors:
Error 1 error C2011: 'Student' : 'class' type redefinition
Error 2 error C2079: 'student1' uses undefined class 'Student'
Error 5 error C2228: left of '.set_name' must have class/struct/union
Error 9 error C2027: use of undefined type 'Student'
How can I go about fixing this issue?
I'm quite sure this is an error caused by the fact that student.h is included twice in a certain .cpp file. Thus you need to use so-called header guards to make sure the file is only included once in every .cpp file:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
/* ... */
};
#endif
The idea behind this is that an #include is a preprocessor directive that results in the argument file being copied into the file where the #include was issued. Hence, if files A and B include Student.h, and file C includes both files A and B, then the declaration of class Student is going to end up duplicated. Hence the error. The above macros make sure that this doesn't happen.
Edit as per the question author's comment:
#pragma once is the same as #ifndef .. #define #endif but non-standard .
See #pragma once vs include guards? for reference.
I had the same error. I just clean and rebuild the solution and error resolved.

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ‘)’ before ‘n’
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

C++ Object inaccessible

I'm trying to reference an object in a function and it is giving me an "Object is inaccessible" error. Here is the header and cpp file in question.
customer header file below. I've put the object declaration at the bottom.
#pragma once
#include "bankAccount.h"
#include "checkingAccount.h"
#include "savingsAccount.h"
#include "address.h"
using namespace std;
class customer {
public:
customer(void);
customer(string,string);
~customer(void);
void setName(string n);
string getName();
void withdrawChecking(double);
void wihdrawSavings(double);
double depositSavings(double);
string print();
private:
string name
checkingAccount myChecking;
savingsAccount mySavings;
};
Here is the cpp file. I've bolded the problem statement.
#include "customer.h"
#include "checkingAccount.h"
customer::customer(void){
}
customer::customer(string n, string ac){
name = n;
mySavings.setAccount(ac);
myChecking.setAccount(ac);
}
void customer::setName(string n){
name = n;
}
string customer::getName(){
return name;
}
void withdrawChecking(double w){
myChecking.withdrawChecking(w);
}
So what is wrong with this last statement and my header?
Sorry for bad styling... first time posting a question.
You're missing a customer on the front of withdrawChecking. It should be:
void customer::withdrawChecking(double w)

What is making my constructor protected?

#ifndef SLIST_H
#define SLIST_H
#include "llist.h"
using namespace std;
class slist:public llist{
public:
slist();
int search(el_t Key);
void replace(el_t Elem, int I);
};
#endif
That is my new class I just made that gives me the search and replace function, on top of all the inherited functions contained in llist.h
In my main...
#include "slist.h"
#include <iostream>
using namespace std;
int main(){
slist list;
list.addFront(4);
cout<<list.search(4);
}
I'm trying to call addfront() which is a public function in the llist class. Then I want to call search() which is an inherited public function of the slist class. g++ gives me a few errors that I don't understand.
slist.h: In function âint main()â:
slist.h:10: error: âslist::slist()â is protected
main.cpp:7: error: within this context
slist() is protected? Why's that? I put it under public:
Also whats up with the this context, I'm guessing I'm just doing the whole inheritance thing totally wrong. Any help would be appreciated!
Edit: Here's the llist class, if it helps
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
class llist{
protected:
typedef int el_t;
el_t total;
struct Node{
int Elem;
Node *Next;
};
Node *Front;
Node *Rear;
Node * Curr;
public:
class Overflow{};
class Underflow{};
class Range{};
llist();
~llist();
bool isEmpty();
void displayAll();
void addRear(el_t NewNum);
void deleteFront(el_t& OldNum);
void addFront(el_t NewNum);
void deleteRear(el_t& OldNum);
void deleteIth(int I, el_t& OldNum);
void addbeforeIth(int I, el_t newNum);
class Overflow;
};
#endif
This is llist.cpp with only the relevant functions pasted
#include "llist.h"
#include <iostream>
using namespace std;
int total=0;
llist::llist(){
Front=NULL;
Rear=NULL;
total=0;
}
llist::~llist(){
while(Front!=NULL){
int z;
deleteFront(z);
}
}
bool llist::isEmpty(){
if(Front==NULL){
return true;
}
return false;
}
void llist::displayAll(){
Curr=Front;
if(isEmpty()){
cout<<"[ empty ]"<<endl;
}else{
while(Curr!=NULL){\
cout<<"curr != NuL"<<endl;
cout<<Curr->Elem<<endl;
Curr=Curr->Next;
}
}
}
void llist::addFront(el_t NewNum){
if(isEmpty()){
Node *x=new Node;
x->Next=Front;
Rear=Front;
Front=x;
Front->Elem=NewNum;
}else{
Node *x=new Node;
x->Next=Front;
Front=x;
Front->Elem=NewNum;
++total;
}
}
I honestly can't see the problem but not every compiler is standard-compliant, so I would try the following:
1) Rename your class - if it works, that means it's a because of a naming conflict.
2) Remove the using directives.
3) Remove the inheritance. If it works after this... you really need to change compilers.
4) Try #undef public before your class declaration. If it works after this... well, someone's in for a talk with the manager.
5) Pray...