I was wondering if this code is correct syntax for function parameters in a class?

class myclass
{
     private:
         char mystring[50];
         int length(char string[]);
     
     public:
         int yourlen(){
             return length(_string, _string);

I know later in my code, I would have something like this:

mystring A;
char string[50], string2[50];
a.youlen(string, string2);

Recommended Answers

All 6 Replies

Not even close.

You have declared myclass's length() method as taking one argument, and yourlen() calls it with two arguments.

Similarly, yourlen() is declared with no arguments, but when you call it you pass two to it. (I'll ignore the typo of calling a.youlen() rather than a.yourlen()).

If you declare a function taking n arguments, you have to pass exactly n arguments to it.

If you had tried to compile your code, your compiler would have complained bitterly.

I was wondering if this code is correct syntax for function parameters in a class?

class myclass
{
     private:
         char mystring[50];
         int length(char string[]);
     
     public:
         int yourlen(){
             return length(_string, _string);

I know later in my code, I would have something like this:

mystring A;
char string[50], string2[50];
a.youlen(string, string2);

Secondly You have not declared 'A' as a myclass but as mystring, which is again unknown to the compiler.

and again you use "a.youlen" , There is a difference between variable "A " and "a" So a is just unrecognised again to your compiler.

Well crap I messed everything up. Sorry, that was a bad and wrong example all around. Scratch my previous thread. This is what I meant:

class myclass
{
     private:
         char mystring[50];
         int test(char string[]);
     
     public:
         int yourtest(){
             return test(_string, _string);
};
//Farther down in my code
.
.
.
.
myclass A;
char string[50], string2[50];
A.yourtest(string, string2);

Well crap I messed everything up. Sorry, that was a bad and wrong example all around. Scratch my previous thread. This is what I meant:

class myclass
{
     private:
         char mystring[50];
         int test(char string[]);
     
     public:
         int yourtest(){
             return test(_string, _string);
};
//Farther down in my code
.
.
.
.
myclass A;
char string[50], string2[50];
A.yourtest(string, string2);

Not Quite There Yet,

in the program after the class declaration(also definition)

A.yourtest(string,string2);

is something like a disaster.

As in the class declaration you have not mentioned arguments to yourtest();

So i guess you would have to give in arguments to yourtest in the definition ,

Something like this would do ,

public:
yourtest(string a,string b)
{
return  test(a,b);
};

But I wouldn't need parameters for that function as long as I define the variables in my constructor?

So then you should try something like having 2 strings defined in your class declaration, And then after creating a new class

assign values to the string and then you will not need the arguments.

If you are not clear on what i have said , consider this program.

class myclass
{
private:
string a;
string b;
test(string,string);
public:
input()
{
cout<<"Enter String 1\n";
cin>>a;
cout<<"\nEnter String 2:\n"
cin>>b;
};
yourtest()
{
return test(a,b);
};
}

Hope this helps.

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.