Dear All
I am trying to create a list of characters already given as below.
When I compile it it gives me following errors.
In function `int main()':
17: error: `islowerCase' undeclared (first use this function)
17: error: (Each undeclared identifier is reported only once for each function it appears in.)
18: error: a function-definition is not allowed here before '{' token
18: error: expected `,' or `;' before '{' token
19: error: `ostream_iterator' undeclared (first use this function)
19: error: expected primary-expression before "char"
19: error: expected `;' before "char"
20: error: `screen' undeclared (first use this function)
listChar.cc:54:2: warning: no newline at end of file
--I would be very happy if someone could help me!
regards
juniper

main()
{       
	vector<string> listChar;
	vector<char> lastElem;
	lastElem = remove_if(listChar.begin(), listChar.end(),islowerCase); 	// Line 1
	bool islowerCase(char c) { return islower(c); }	
	ostream_iterator<char> screen(cout,""); 				// Line 2
	copy(listChar.begin(), lastElem, screen);				// Line 3
	
	
	listChar.push_back("w");   
	listChar.push_back("W");   
	listChar.push_back("b");
	listChar.push_back("B");   
	listChar.push_back("C");   
	listChar.push_back("E");   
	listChar.push_back("w");   
	listChar.push_back("F");   
	listChar.push_back("B");
	listChar.push_back("m");

	cout << " "<< endl;
	cout << "Loop by index:" << endl;   
	int ii;   
	
	for(ii=0; ii < listChar.size(); ii++)   
	{      
		cout << listChar[ii] << endl;   	
	}   
   
	cout << endl << "Reverse Iterator:" << endl;   
	
	vector<string>::reverse_iterator rii;   
	for(rii=listChar.rbegin(); rii!=listChar.rend(); ++rii)   
	{      
		cout << *rii << endl;   
	}   
		cout << endl << "The Number of Characters:" << endl;   
		cout << listChar.size() << endl;   cout << listChar[10] << endl;   
	swap(listChar[0], listChar[10]);  
	cout << listChar[10] << endl;
}

Recommended Answers

All 8 Replies

Move that entire line 6 out of main and put it before and outside of main.

Also, main() should always be int main(), returning 0 at the very end

Okay first things first...
What exactly are you trying to achieve with this code?

Secondly, You can get rid of some of these errors:

1. 17: error: `islowerCase' undeclared (first use this function)
You'll have to write a function definition before attempting to use the function, as it is defined, moving sequentially, below its reference. To make it easier, let it be defined above the main() function. It should also be called as islowerCase(c) // see this: its a function which expects some argument, here, the argument is stored in c 2. 18: error: a function-definition is not allowed here before '{' token
18: error: expected `,' or `;' before '{' token

This problem is addressed above, by moving the function definition of islowerCase(char c) , outside main() 3. 19: error: `ostream_iterator' undeclared (first use this function)
19: error: expected primary-expression before "char"
19: error: expected `;' before "char"
20: error: `screen' undeclared (first use this function)

This problem may arise if you haven't included the header file <iterator> . Here's the minimal list of headers you need to have for this program:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

4. Apart from these errors, there are some logical errors, like:
a) You cannot equate vector<string> with a vector<char> , like you did on line 5 of the program you posted, i.e lastElem = remove_if(listChar.begin(), listChar.end(),islowerCase); // Line 1 b) This cannot be done: copy(listChar.begin(), lastElem, screen); // Line 3 If you do this, then the compiler will complain that there's no matching function with such a signature...

c) Add a return 0; to the end of the main() function, although, I believe that some compilers add this automatically(?), but its good practice to write it anyway....

Hope this helped!

EDIT:
@jonsca : You can write function definitions without declaring its return-type before-hand, as most compilers treat such functions as integer-returning functions, so here, main() will be taken up to return an integer. So basically, main() will translate to int main() , automatically. This is true for all functions as well.

commented: it was helpfull +1

Dear Amrith92
First of all Thank you so much for your quick help

i am supposing that charList is declared as a vector containing a list ofcharacters. I nedd to Insert the list of characters in the order given: w, W, b, B, C, E, w, F, B, m, into the vector container, charList.
you want use a push_back() function to insert each character or populate the vector with elements from an array.

Further I am supposing that the following two lines of code are added immediately
after the push_back()operations above:
lastElem = remove_if(charList.begin(), charList.end(),
islowerCase); // Line 1
ostream_iterator<char> screen(cout, “ “); // Line 2

where:
· lastElem is declared as an iterator for a vector container of the type char;

· islowerCase is declared as follows:
bool islowerCase(char c) { return islower(c); }.
Finally add the statement below and briefly explain the output:
copy(charList.begin(), lastElem, screen); // Line 3

HERE IS THE LATEST VERSION OF MY PROGRAM;

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>

using namespace std;

lastElem = remove_if(listChar.begin(), listChar.end(),islowerCase); 	// Line 1

bool islowerCase(char c) 
{ 
  return islower(c); 
}

int main()
{       
	vector<string> listChar;
	vector<char> lastElem;
	ostream_iterator<char> screen(cout,""); 				// Line 2
	copy(listChar.begin(), lastElem, screen);				// Line 3
	
	
	listChar.push_back("w");   
	listChar.push_back("W");   
	listChar.push_back("b");
	listChar.push_back("B");   
	listChar.push_back("C");   
	listChar.push_back("E");   
	listChar.push_back("w");   
	listChar.push_back("F");   
	listChar.push_back("B");
	listChar.push_back("m");

	cout << " "<< endl;
	cout << "Loop by index:" << endl;   
	int ii;   
	
	for(ii=0; ii < listChar.size(); ii++)   
	{      
		cout << listChar[ii] << endl;   	
	}   
   
	cout << endl << "Reverse Iterator:" << endl;   
	
	vector<string>::reverse_iterator rii;   
	for(rii=listChar.rbegin(); rii!=listChar.rend(); ++rii)   
	{      
		cout << *rii << endl;   
	}   
		cout << endl << "The Number of Characters:" << endl;   
		cout << listChar.size() << endl;   cout << listChar[10] << endl;   
	swap(listChar[0], listChar[10]);  
	cout << listChar[10] << endl;
}

Thanks again

Now I have got only one error as you mentioned at line 21 e.g:copy(listChar.begin(), lastElem, screen);

what should I replace it with?

I'm glad it helped! :)

I'm still not very sure what you want isLowerCase for, and how it should be used in your program, but, here's how I understand your program:
1. Load the 10 characters into a character vector
2. Copy it into the screen buffer
3. Print it in order, and then in reverse
4. Print its size(1), last index and its last index after swapping the first index with the last.

Here's the code for that. I've removed the string vector, and some other statements that didn't make sense to me....

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
    vector<char> listChar;
    ostream_iterator<char> screen(cout,""); 				// Line 2
    copy(listChar.begin(), listChar.end(), screen);				// Line 3


    listChar.push_back('w');
    listChar.push_back('W');
    listChar.push_back('b');
    listChar.push_back('B');
    listChar.push_back('C');
    listChar.push_back('E');
    listChar.push_back('w');
    listChar.push_back('F');
    listChar.push_back('B');
    listChar.push_back('m');

    cout << " "<< endl;
    cout << "Loop by index:" << endl;
    int ii;

    for (ii=0; ii < listChar.size(); ii++)
    {
        cout << listChar[ii] << endl;
    }

    cout << endl << "Reverse Iterator:" << endl;

    vector<char>::reverse_iterator rii;
    for (rii=listChar.rbegin(); rii!=listChar.rend(); ++rii)
    {
        cout << *rii << endl;
    }
    cout << endl << "The Number of Characters:" << endl;
    cout << listChar.size() << endl;
    cout << listChar[listChar.size()-1] << endl;
    swap(listChar[0], listChar[listChar.size()-1]);
    cout << listChar[listChar.size()-1] << endl;

    return 0;
}

Hope this helped!

Thank you so much for your help
Have a good weekend

@jonsca : You can write function definitions without declaring its return-type before-hand, as most compilers treat such functions as integer-returning functions, so here, main() will be taken up to return an integer. So basically, main() will translate to int main() , automatically. This is true for all functions as well.

I understand what you are saying but it is a vestigial feature and at best not a good habit to get into.

I understand what you are saying but it is a vestigial feature and at best not a good habit to get into.

Agreed :) I wasn't defending the practice, but I was merely substantiating that it wasn't the problem juniper2009 was facing in the code provided...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.