LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2014, 09:58 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 98

Rep: Reputation: Disabled
compile and run c++ which contains only class and functions inside it


Hi
I am having this code which I got from coding competition contest.I understood the logic but finding difficult to compile and run using g++ in terminal.Normally every code contains main(), then how to write this code with main function.Please guide me.

Assume proper headers are included.
Code:
using namespace std;
class BinaryCode
{
private:
	void decode_one(string &message, int first, string &res)
	{
		const size_t size = message.size();
		if(size == 1)
		{
			res = (message[0] == first ? message :"NONE");
			return;
		}	

		int *r;
		size_t i;
		r = new int[size];
		r[0] = first;
		
		for(i = 1; i < size ;i++)
		{
			r[i] = message[i] - ((i - 2 >=0 ? r[i - 2]: 0) + r[i-1]);
		}

		if(r[size - 2] + r[size - 1] != (message[size - 1]))
		{
			res = "NONE";
			return;
		}
		
		cout  << res << endl;
		res.resize(size);
		for( i =0 ; i < size ; i++)
		{
			if(r[i] != 0 && r[i] != 1)
			{
			res = "NONE";
			return;
			}
			res[i] = r[i] + '0';
			
		}
		delete r;

	}
	
public:
	vector <string> decode(string message)
	{
		vector<string>r(2);
		if(!message.empty())
		{
			decode_one(message,0,r[0]);
			decode_one(message,1,r[1]);
		}
		return r;
	}
};
 
Old 07-13-2014, 11:03 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 463

Rep: Reputation: 192Reputation: 192
Uh? Write main function which creates an object and calls decode method or whatever else the program is supposed to do. I'm not sure what kind of answer you expect to get.
 
1 members found this post helpful.
Old 07-13-2014, 11:56 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,236

Rep: Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344
This seriously wasn't obvious?

Code:
int main()
{
    cout << BinaryCode().decode("message") << endl;
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ - Using class pointers to use class functions? golmschenk Programming 2 04-24-2011 12:41 AM
Adding new functions in string class linux_chris Linux - Software 2 02-22-2009 02:59 PM
PHP: Calling class functions from another class. unreal128 Programming 5 11-24-2006 03:04 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
Need help with C++ class functions :( starla827 Programming 4 03-26-2004 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration