#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

void x();
void x(char);
void x(char,int);
void x(char,int,char);
void x(int);
void x(char);

int main(int argc, char *argv[])
{
x();
x('B');
x('E',30);
x('B',30,'E');
x(10);
x('V');
getch();
}

void x()
{
     for(int j=0;j<50;j++)
     cout << '*';
     cout << endl;
     }
void x(char xx)
{
 for(int j=0;j<50;j++)
     cout << xx;
     cout << endl;
     }
 void x(char xx,int yy)
 {
 for(int j=0;j<yy;j++)
     cout << xx;
     cout << endl;
     }
void x(char xx,int yy, char pi)
 {
  for(int j=0;j<yy;j++)
     cout << xx << "."<<pi;
     cout << endl;
     }
     
void x(int yy)
 {
  for(int j=0;j<yy;j++)
     cout << j << ".";
     cout << endl;
     }     
     
 void x(char xx)
{
 for(int j=0;j<50;j++)
     cout << xx;
     cout << endl;
     }

Dani AI

Generated

Short answer: the compiler error comes from having two definitions with the exact same parameter list. As said, you cannot provide two functions that share the exact signature; was also right to ask why you had duplicate declarations. The fix is to keep a single definition per signature or change the parameter types/names so the signatures differ.

A few concrete points that clarify the rule and help avoid surprises:

  • A function signature for overloading is determined by the parameter types (and their order). You cannot overload only by return type. Re-declaring the same signature more than once is allowed only as a compatible declaration; providing two separate full definitions triggers a redefinition/ODR error.
  • Top-level const on by-value parameters is ignored for the signature (so int and const int are the same). Pointer/reference const-ness (e.g., const char* vs char*) is part of the type and can be used to overload.
  • String literals are arrays of const char and decay to const char*. If a call uses a string literal, provide an overload that accepts const char* or std::string to avoid unexpected conversions.

Practical fixes and a small example pattern to follow:

  • Remove the duplicate definition of the same signature. If the two functions should do different things, change one parameter type or rename the function.
  • Prefer expressive names instead of reusing a single name for unrelated behavior.
  • Example overload pattern (different names than your original) that avoids the duplicate-signature problem:
void printLine(char ch);            // for single-character fills
void printLine(const char* text);   // for C-style string input
void printLine(const std::string& s); // for std::string callers

Also note: conio.h and getch() are nonstandard; for portable code use std::cin.get() if you need to pause.

For authoritative details on overload resolution and definitions, see the C++ reference on overload resolution: C++ overload resolution.

Thanks to and for spotting the core issue; it looks like switching to a string-accepting overload or removing the duplicate resolved it.

Recommended Answers

All 3 Replies

So what exactly is wrong? And your two x(char) functions both do exactly the same thing, so why do you have two of them declared?

This won't compile because function x is having two overloaded versions with same signature -

void x(char)

It should produce an error "Function Re-Declared" .. something like that.

thank you. I write xx (char) or x(string) it is working. I understand that.

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.