3 Replies - 1834 Views - Last Post: 23 September 2012 - 10:16 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Not able to use method in other class (C++ newbie)?

Posted 23 September 2012 - 09:08 AM

I had LinkedList class as a subclass in my main and everything seemed to have worked fine.
I just copy-pasted my LinkedList into another source file LinkedList.cpp.
Inluded the file in my main.

Problem: When I compile, this is the error I get:
1>------ Build started: Project: LinkedList, Configuration: Debug Win32 ------
1>  FilterData.cpp
1>LinkedList.obj : error LNK2005: "public: void __thiscall LinkedList::Add(int)" (?Add@LinkedList@@QAEXH@Z) already defined in FilterData.obj
1>LinkedList.obj : error LNK2005: "public: void __thiscall LinkedList::PrintData(void)" (?PrintData@LinkedList@@QAEXXZ) already defined in FilterData.obj
1>C:\Users\Karan\documents\visual studio 2010\Projects\LinkedList\Debug\LinkedList.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



#include "LinkedList.cpp"
#include <iostream>
#include <cstring> // definitions for c string functions.
#include <fstream>
#include <sstream>
using namespace std;

int main(){

	LinkedList ll;
	int more;

	ll.Add(78);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(400);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(400);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(78);
	ll.Add(200);
	ll.Add(78);
	ll.Add(300);
	ll.Add(400);
	ll.PrintData();

	cout << "Do you want to exit" << endl;
	cin >> more;
	return 0;

}


Below is my LinkedList.cpp_______________________

#include <iostream>
#include <cstring> // definitions for c string functions.
#include <fstream>
#include <sstream>

using namespace std;
class Node {
public:
	int number;
	Node* next;
	

	public: 
	void setData (int num){
		number = num;
	};
	void setNext (Node* myNext){
		next = myNext;
	};

	int getData(){
		return number;
	};

	Node* myNext(){
		return next;
	};
};

class LinkedList{
	Node *head;
public:
	LinkedList(){
		head = NULL;
	};
	
public:
	void Add (int data);
	void Delete (int data);
	void PrintData();
	void check(int data);
};


void LinkedList::Add(int data){

	Node *temp = head;

	if(head == NULL){
		Node* newNode = new Node();
	(*newNode).setData(data);
	(*newNode).setNext(NULL);
		head = newNode;
	}
	else{
		
		while((*temp).myNext() != NULL){

			if((*temp).getData() == data){
				(*temp).setData(data);
				break;
			}
			else{
			temp = temp->myNext();
			}
		}
			if((*temp).myNext() == NULL && ((*temp).getData() != data)){
			Node* newNode = new Node();
			(*newNode).setData(data);
			(*newNode).setNext(NULL);
			(*temp).setNext(newNode);
		}
	}

};

void LinkedList::PrintData(){
	Node *temp = head;
	if(temp == NULL){
		cout << "No Data" << endl;
		return;
	}
	else{
		if((*temp).myNext()==NULL){
			
			cout << (*temp).getData() << endl;
		}
		else{
			while(temp != NULL){
				cout << (*temp).getData() << endl;
				temp = (*temp).myNext();
			}
		}

	}
};

This post has been edited by deprosun: 23 September 2012 - 09:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Not able to use method in other class (C++ newbie)?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Not able to use method in other class (C++ newbie)?

Posted 23 September 2012 - 09:23 AM

Normally you never include a C++ source file. You add that file to your project. You normally create a header file that contains the class definition and a .cpp file that contains the implementation. You would then include the header file in both your main.cpp file and the implementation .cpp file. Don't forget to add include guards to your header.

Jim

This post has been edited by jimblumberg: 23 September 2012 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: Not able to use method in other class (C++ newbie)?

Posted 23 September 2012 - 09:34 AM

View Postjimblumberg, on 23 September 2012 - 11:23 AM, said:

Normally you never include a C++ source file. You add that file to your project. You normally create a header file that contains the class definition and a .cpp file that contains the implementation. You would then include the header file in both your main.cpp file and the implementation .cpp file. Don't forget to add include guards to your header.

Jim


I see. But I have to create a separate LinkedList.cpp class as my project. I had my LinkedList class over my main. Works fine. How can I still run it, but have LinkedList.cpp as a different class that main uses? I just want to be able to use all of its methods from my main.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Not able to use method in other class (C++ newbie)?

Posted 23 September 2012 - 10:16 AM

Shameless plug for a blog post of mine that explains the cpp compilation process.

In a nutshell, main includes the header for the other class and then the .o from the other cpp file is linked to main so main knows where to find the implementation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1