Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tested a demo from this link to test a using namespace statement. I got this error message:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C2664	'void One::funct1(char)': cannot convert argument 1 from 'const char [15]' to 'char *'	namespace04	c:\demo\namespace04\namespace04\main.cpp	35	


it seems like
void funct1(char *str)
is not recognized.

here is the full code for your convenience:
//explicit access, namespace within a class
#include <iostream>
using namespace std;

class One
{
	public:
		void funct1(char chs)
		{
			cout << "character = " << chs << endl;
		}
};

class Two :public One
{
	public:
		//The using directive is not allowed in class using namespace One;
		void funct1(char *str)
		{
			cout << "String = " << str << endl;
		}

		// using declaration is OK in class
		using One::funct1;  // overload Two::funct1()
};


int main(void)
{
	Two Sample;
	// calling One::funct1()
	Sample.funct1('P');

	// calling Two::funct1()
	Sample.funct1("This is string"); <==== error line is this statement

	return 0;

}


What I have tried:

tried, but I can not find the theoretical reason for this error...
Posted
Updated 26-Sep-20 8:54am
v2

1 solution

Two derives from One. Both declare the function funct1. The declaration in Two hides the declaration in One, so it is no longer accessible.

EDIT1: I compiled it successfully, so I don't know what to tell you. It's poor practice to overload a function name this way, so I don't know if I'll keep playing with this.

EDIT2: See this[^]. Your using statement is for a class member, not a namespace, and the relevant explanation is
Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived. In this case, nested-name-specifier must name a base class of the one being defined. If the name is the name of an overloaded member function of the base class, all base class member functions with that name are introduced. If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
I don't know how to interpret, in the last sentence, hides or overrides (does not conflict with). Yours isn't a proper override, because the function isn't virtual. So the sentence seems to imply that it hides, but does not conflict with, the base class name. It makes no sense to me.
 
Share this answer
 
v6
Comments
Southmountain 26-Sep-20 15:02pm    
but the argument types are different, it should be okay..
Greg Utas 26-Sep-20 15:12pm    
No, it is not OK, although it could be that the using statement allows it to compile successfully (I'm using VS2017). Whether this is compliant to the spec is another question. It seems useful, but the above quote suggests that it may not be portable. Moreover, why do you want to do this? Just use a different name.
Southmountain 26-Sep-20 15:16pm    
thank you very much for your time! I understand your answer now.

for your EDIT1, what did you do to let it compiled? I am curious to know...
Greg Utas 26-Sep-20 15:20pm    
I only deleted the <==== "comment" on the line where you got an error.
Greg Utas 26-Sep-20 15:21pm    
You had to comment out "using namespace One" because One is a class, not a namespace.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900