Why is n an undeclared identifier

Started by
13 comments, last by rip-off 16 years, 8 months ago
Hi everyone I'm doing a bubble sort and getting an error I don't understand, how do I fix it please? Here's my code:


#include <iostream>
using namespace std;

void BubbleSort(int data[], int n);

int main()
{
	int data[10];
	int input = 0;
	cout << "Enter ten unsorted integers: " << endl;

	for(int i = 0; i < 10; ++i)
	{
		cout << "[" << i << "] = ";
		cin >> input;
		data = input;
	}

	cout << endl << "Unsorted List = ";

	for(int i = 0; i < 10; ++i)
	{
		if(i < 9)
		{
			cout << data << ", ";
		}
		else
		{
			cout << data;
		}
	}

	cout << endl << "Sorting..." << endl << "Sorted List = ";
	cout << BubbleSort(data, n) << endl << endl;
}

void BubbleSort(int data[], int n)
{
	int subArrayEnd = 9;

	while( subArrayEnd > 0 )
	{
		int nextEnd = 0;

		for(int j = 0; j < 8; ++j)
		{
			if( data[j] > data[j+1] )
			{
				int temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;
				nextEnd = j;
				subArrayEnd = nextEnd;
			}
			else
			{
				data[j] = data[j];
				data[j+1] = data[j+1];
			}
		}
	}
}


I get this error:


------ Build started: Project: 3.7.8, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.8\3.7.8\main.cpp(34) : error C2065: 'n' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.8\3.7.8\Debug\BuildLog.htm"
3.7.8 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks, Chris.
Advertisement
int main(){	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";        // You are using n here, but look above, you never declared it!	cout << BubbleSort(data, n) << endl << endl; }
Best regards, Omid
I know this may be the simple response, but maybe because you never identified it.

You don't even seem to use it... but the error is caused by you never declaring it (like "int n;") and never defining it either (like "n = 5;")*

Yes I know the question was just answered, but it was answered in code, in a comment, and I didn't know if that was obvious.


*or "int n = 5;" from the start, yes
This man made my day: http://www.gamedev.net/community/forums/topic.asp?topic_id=523021
Hi, thank you for your replies.

I've now adjusted my function so that I'm actually using n, should have realised that one myself lol!

Sorry, I don't think I explained myself in my first post.

I thought I'd already declared it when I originally put it in the function declaration at the top since isn't that global relative to where it comes up as an undeclared identifier?

I realise that I hadn't defined it there but when I put int n = 10 at the start of main, I got another error plus a load of other stuff.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[], int n);int main(){	int n = 10;	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	cout << BubbleSort(data, n) << endl << endl;}void BubbleSort(int data[], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd - 1; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;				subArrayEnd = nextEnd;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}	}}


In the output, I get:

------ Build started: Project: 3.7.8, Configuration: Debug Win32 ------Compiling...main.cppc:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.8\3.7.8\main.cpp(35) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)        c:\program files\microsoft visual studio 8\vc\include\ostream(656): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(703): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(741): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(788): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(912): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(919): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(926): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(933): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(180): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(187): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(194): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(214): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(247): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(267): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(292): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(312): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(332): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(353): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(373): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(394): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(414): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(434): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(454): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(474): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        while trying to match the argument list '(std::ostream, void)'Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.8\3.7.8\Debug\BuildLog.htm"3.7.8 - 1 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
int main() {    // snip except last line    cout << BubbleSort(data, n) << endl << endl;}void BubbleSort(int data[], int n) {// ...


std::cout doesn't know how to << something of type "void".

The solution would be to call BubbleSort, and then print all the elements of the array.

Here is the important part of the error messages:
Quote:
binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)


However I understand that template error messages are extremely difficult to read when you are unused to the language.
Hi, thank you for your reply.

Does this involve using function overloading to define a new function that just prints the list out? If so, how do I need to define my function and what do I need it to execute. If not, I'm not sure what you mean.

Thanks,
Chris.
Quote:Original post by chockydavid1983
Hi, thank you for your reply.

Does this involve using function overloading to define a new function that just prints the list out? If so, how do I need to define my function and what do I need it to execute. If not, I'm not sure what you mean.

Thanks,
Chris.


No, you don't have to do that.

See where you printed the "Unsorted List". Just do the same after you sort the list [smile]. You can move the printing to a separate function too.
A note: just because your function takes an integer named 'n', doesn't mean you have to pass it an integer named 'n' also - 'n' is just how the integer that you pass it is named inside that function. So:

void SomeFunction(int n);int main(){	// Inside SomeFunction, n will be 5	SomeFunction(5);	// Inside SomeFunction, n will be 8 - SomeFunction doesn't even know that	// that 8 came from a variabele named a, and that's just fine. :)	int a = 8;	SomeFunction(a);	// Note that this integer, named n, is a different integer than used inside	// SomeFunction - they have the same name, but are within different scopes	int n = 12;	SomeFuntion(n);}void SomeFunction(int n){	// Blabla}
Create-ivity - a game development blog Mouseover for more information.
Hi, thanks for your replies.

I seem to have got a bit lost. I am using the right method here? I'm trying to sort the integers using the function and then output them as a list using the for loop.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[], int n);int main(){	int n = 10;	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	BubbleSort(data, n);	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}}void BubbleSort(int data[], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd - 1; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;				subArrayEnd = nextEnd;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}	}}


Thanks,
Chris.
Quote:
I am using the right method here?


Pretty much.

Does the program do what you want? If so, then you are right on target. [grin]

If not, then what isn't it doing...

A program can always be improved. For example, the code you've used to print the array is used twice. Moving the code into a separate function means there isn't redundant code, and if you want to change anything you only have to make the change in one place.

This topic is closed to new replies.

Advertisement