USING DEFAULT ARGUMENTS

Any ideas to solve this error ..

#include<iostream>
using namespace std ;
void repchar(char='*', int=45) ;
int main()
{
repchar();
repchar('=') ;
repchar('+',30);
return 0 ;
}

void repchar(char ch , int n)
{
for(int j=0;j<n;j++)
cout<<ch;
cout<<endl ;
}

My question is how to use the same conscept within a ncurses program ?

I tried it once , but I cant understand the error. (error:undefined reference to `repchar(char, int)')
#include<ncurses.h>
void repchar(char='*' , int=10) ;

int main()
{
initscr();
repchar();
repchar('+',30) ;
getch();
endwin();
return 0 ;
}

void repchar(char ch[] , int n)
{

for(int j=0;j<n;j++)
{
move(j,j) ;
printw(ch);
}
}

Any suggestions ?
Thank You .!!
In the second code you have declared repchar to take a char as the first argument but defined it to take a pointer to a char as argument.
Ya thats a problem in this code , So I need your help to correct the error. I mean how to make both, pointers or how to make both as just an argument.(Its not working for me/ Error in my way)

any suggestions ?

Thank You ..!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void repchar( char = '*' , int = 10 ) ; // overload for char
void repchar( const char* , int = 10 ) ; // overload for const char*

int main()
{
    repchar( '+', 30 ) ;
    repchar( "+=", 15 ) ;
}

void repchar( char ch , int n )
{ const char cstr[] = { ch, 0 } ; repchar( cstr, n ) ; }

void repchar( const char* cstr , int n )
{ for( int j=0 ; j<n ; j++ ) { move( j, j ) ; printw(cstr) ; } }

Last edited on
Note that for strings you use double quotes so that will look somethin like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<ncurses.h>
void repchar(const char*="*" , int=10) ;

int main()
{
	initscr();
	repchar();
	repchar("+",30) ;
	getch();
	endwin();
	return 0 ;
}

void repchar(const char ch[] , int n)
{

	for(int j=0;j<n;j++)
	{
		move(j,j) ;
		printw(ch);
	}
}


If you just want to pass a single char you use single quotes as you did. You will have to substitute printw with a function that takes a char as argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<ncurses.h>
void repchar(char='*' , int=10) ;

int main()
{
	initscr();
	repchar();
	repchar('+',30) ;
	getch();
	endwin();
	return 0 ;
}

void repchar(char ch , int n)
{

	for(int j=0;j<n;j++)
	{
		move(j,j) ;
		addch(ch);
	}
}
Great ..! It works .. but my question is why we should make it const,

why the compiler isn't allowing to compile without const ?


Thank You .!!
> but my question is why we should make it const,

See: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1
Topic archived. No new replies allowed.