Another compilation error!

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
someone2
New poster
Posts: 10
Joined: Tue Jul 05, 2005 5:22 pm

Another compilation error!

Post by someone2 » Wed Jul 27, 2005 11:52 am

Hi,
Well, I just got another compilation error for a perfectly valid code. Since, I don't have any older versions of gcc, I was wondering if someone could, please try it and tell me what is wrong.

here is the code.

Code: Select all

#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <functional>
#include <strstream>
#include <queue>
#include <cassert>
#include <string>

using namespace std;

//#define ONLINE_JUDGE

#ifdef ONLINE_JUDGE
 #define Fin stdin
 #define Fout stdout
#else
 #define Fin f1
 #define Fout f2
#endif

FILE* f1;
FILE* f2;

struct node
{
	int freq;
	int symbol;
	int id;
	vector<node*> children;
};

struct result_node
{
	string s;
	char symbol;
};

struct resultCompare:binary_function<result_node,result_node,bool>
{
	bool operator()(result_node x,result_node y)
	{
		if(x.symbol<y.symbol)
			return 1;
		return 0;
	}
};

struct nodeCompare:binary_function<node,node,bool>
{
	bool operator()(node x,node y)
	{
		if(x.freq>y.freq)
			return 1;
		else if(x.freq==y.freq)
		{
			if(x.id>y.id)
				return 1;
		}
		return 0;
	}
};

vector<node> symbols;
int R;
int N;
int id;
long double average;
int sumStuff;

priority_queue<node,vector<node>,nodeCompare> huff;
priority_queue<node,vector<node>,nodeCompare> emptyOne;

vector<result_node> results;
void inOrder(node* curNode,string s)
{
	if(curNode->children.size()==0)
	{
		if(curNode->symbol!=-1)
		{
			result_node v;
			v.s=s;
			v.symbol=curNode->symbol+'A';
			results.push_back(v);
			average+=s.length()*curNode->freq;
			sumStuff+=curNode->freq;

		}
	}
	else
	{
		for(int i=0;i<curNode->children.size();i++)
		{
			string newS=s+(char)(i+'0');
			const char* v=newS.c_str();
			inOrder(curNode->children[i],newS);
		}

	}
}

int setNo;
void printCodes()
{
	average=0;
	sumStuff=0;

	results.clear();
	node ourNode=huff.top();
	inOrder(&ourNode,"");
	
	sort(results.begin(),results.end(),resultCompare());

	if(results.size()>0)
		average=average/sumStuff;
	fprintf(Fout,"Set %d; average length %.2Lf\n",++setNo,average);
	
	for(int i=0;i<results.size();i++)
	{
		fprintf(Fout,"    %c: %s\n",results[i].symbol,results[i].s.c_str());
	}
	fprintf(Fout,"\n");
}


void solve(void)
{
	while(1)
	{
		id=0;
		huff=emptyOne;
		symbols.clear();
		char data[10000];
		fgets(data,10000,Fin);
		istrstream str(data);
		str>>R>>N;
		if(R==0)
			break;
		int i;
		for(i=0;i<N;i++)
		{
			node v;
			v.symbol=i;
			str>>v.freq;
			v.id=id++;
			symbols.push_back(v);
		}

		//construct the inital
		for(i=0;i<N;i++)
			huff.push(symbols[i]);

		//add fictitous nodes!!
		node v;
		v.freq=0;
		v.symbol=-1;
		if(N<R&&R>2)
		{
			for(int j=0;j<R-N;j++)
				huff.push(v);
		}
		else if(R>2)
		{
			int l=N-R;
			if(l%(R-1)!=0)
			{
				int s=(R-1)-(l%(R-1));
				for(int j=0;j<s;j++)
					huff.push(v);
			}
		}


		//do huffman!
		while(1)
		{
			if(huff.size()==1)
			{
				//we are done!
				printCodes();
				break;
			}
			else
			{
				assert(huff.size()>=R);
				node* ourNewNode=new node;
				ourNewNode->id=id++;
				ourNewNode->freq=0;
				for(int i=0;i<R;i++)
				{
					node v=huff.top();
					node* ptr=new node;
					*ptr=v;
					huff.pop();
					ourNewNode->children.push_back(ptr);
					ourNewNode->freq+=ptr->freq;
				}
				huff.push(*ourNewNode);
			}
		}
	}
}



int main(void)
{
#ifndef ONLINE_JUDGE
  f1=fopen("cur.in","r");
  f2=fopen("cur.out","w");
#endif

  solve();

#ifndef ONLINE_JUDGE
 fclose(Fin);
 fclose(Fout);
#endif
  return 0;
}
And btw, is there anyway I can check the validity of the code myself? I am using Windows. What compiler should I install to have the exact environment as the judge? Thank you very much in advance... :)

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Wed Jul 27, 2005 1:54 pm

I got the following compile error when I compiled your code using g++ 2.95:

In function `void printCodes()':
implicit declaration of function `int sort(...)'

I think you forgot to #include <algorithm>

someone2
New poster
Posts: 10
Joined: Tue Jul 05, 2005 5:22 pm

Post by someone2 » Thu Jul 28, 2005 7:57 am

Thank you very much! :) I finally got accepted!! :)

Post Reply

Return to “C++”