I am trying to call the method ip_uri from class into the main but it does let me what am i doing wrong?

#include <iostream>
#include <string>

using namespace std;

class ip_uri_store
{
public:
    string count;       // C++ string s are classes so this is aggregation.
    int id ;               // Such strings are more powerful and easier to use
    ip_uri_store *next;
    string ip_uri(string ip50, string uri50);

protected:
string ip50;
string uri50;

};
ip_uri_store *list_head ;  // Start of linked list, set to 1st item.
ip_uri_store *work_ptr;

string ip_uri_store::ip_uri(string ip50, string uri50)
{
if(ip50=="74.125.19.1")
uri50="www.google.com";
cout<<uri50<<endl;
return(uri50);

};
int main(int argc, char *argv[])
{
list_head = NULL;
 int i = 0 ;
   while ( i < argc )
    { work_ptr = list_head ;          // save old head item.
      list_head = new ip_uri_store;  // create new item.
      list_head->next = work_ptr ;    // get next item to point to previous 
      list_head->count = argv[i] ;
      list_head->id = i ;         // fill in payload data.
      i++ ;
    }
 work_ptr = list_head ;          // point to start of list.
   while( work_ptr != NULL)        // not at end.
    { cout << "   " << work_ptr->id << "  " << work_ptr->count<< endl ;
      work_ptr = work_ptr->next ;  // point to next item in list, or NULL.
    }


char ch;
for (int k = 1; k < 2; ++k) {
    for (int j = 0; argv[k][j] != '\0'; ++j) {
        ch = argv[k][j];
}
}
cout<<ch<<endl;
if(ch=='U')
{
string ip50=argv[2];
ip_uri_store google;
google.ip_uri(ip50);
}

return(0);
}

Recommended Answers

All 9 Replies

member function string ip_uri(string ip50, string uri50); expects 2 string parameters, not 1 as you have given.

so can i pass a blank parameter? or does it have to have a value?

you can give it a default parameter of a blank string
line 12: string ip_uri(string ip50, string uri50 = "");

but i still want to be able to use string uri50?!!

Try this:

string output = google.ip_uri(ip50, "");
cout << output << "\n";

instead of this: google.ip_uri(ip50); You should also check if argv has any values, else your program will crash when ran without any param,s.

It is working okay now but i have a little problem i get an error msg after the output which says:
./final U www.google.com
74.125.19.1

terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Aborted (core dumped)

#include <iostream>
#include <string>

using namespace std;

class ip_uri_store
{
public:
    string count;       // C++ string s are classes so this is aggregation.
    int id ;               // Such strings are more powerful and easier to use
    ip_uri_store *next;
    string ip_uri(string ip50, string uri50);
    string uri_ip(string uri50, string ip50);

protected:
string ip50;
string uri50;

};
ip_uri_store *list_head ;  // Start of linked list, set to 1st item.
ip_uri_store *work_ptr;

string ip_uri_store::ip_uri(string ip50, string uri50)
{
if(ip50=="74.125.19.1")
{
uri50="www.google.com";
cout<<uri50<<endl;
return(0);
}
else if(ip50=="74.1.2.3")
{
uri50="www.abc.net";
cout<<uri50<<endl;
return(0);
}
return(0);
};

string ip_uri_store::uri_ip(string uri50, string ip50)
{
if(uri50=="www.google.com")
{
ip50="74.125.19.1";
cout<<ip50<<endl;
return(0);
}
else if(uri50=="www.abc.net")
{
ip50="74.1.2.3";
cout<<ip50<<endl;
return(0);
}
return(0);
};
int main(int argc, char *argv[])
{
list_head = NULL;
 int i = 0 ;
   while ( i < argc )
    { work_ptr = list_head ;          // save old head item.
      list_head = new ip_uri_store;  // create new item.
      list_head->next = work_ptr ;    // get next item to point to previous 
      list_head->count = argv[i] ;
      list_head->id = i ;         // fill in payload data.
      i++ ;
    }
 work_ptr = list_head ;          // point to start of list.
   while( work_ptr != NULL)        // not at end.
   { //cout << "   " << work_ptr->id << "  " << work_ptr->count<< endl ;
     work_ptr = work_ptr->next ;  // point to next item in list, or NULL.
    }


char ch;
for (int k = 1; k < 2; ++k) {
    for (int j = 0; argv[k][j] != '\0'; ++j) {
        ch = argv[k][j];
}
}
//cout<<ch<<endl;
if(ch=='I')
{
string uri50;
string ip50=argv[2];
ip_uri_store ipuri;
ipuri.ip_uri(ip50, uri50="");
}
else if(ch=='U')
{
string ip50;
string uri50=argv[2];
ip_uri_store urip;
urip.uri_ip(uri50,ip50="");
}

return(0);
}

but i still want to be able to use string uri50?!!

What I posted doesn't prevent you from doing that. All it does is provide a "" value when the calling function omits that parameter.

Line 56 would then become: ipuri.ip_uri(ip50);

You have a mismatch in some of the functions, they are declared as returning a string but you are returning (0). Either return a string from those functions if you need to, or maybe change to e.g.

void ip_uri_store::uri_ip(string uri50, string ip50)

if you will not be needing a string returned from these functions.

Thanyou sooo much for your help its working fine now :-D

#include <iostream>
#include <string>

using namespace std;

class ip_uri_store
{
public:
    string count;       // C++ string s are classes so this is aggregation.
    int id ;               // Such strings are more powerful and easier to use
    ip_uri_store *next;
    void ip_uri(string ip50, string uri50);
    void uri_ip(string uri50, string ip50);

protected:
string ip50;
string uri50;

};
ip_uri_store *list_head ;  // Start of linked list, set to 1st item.
ip_uri_store *work_ptr;

void ip_uri_store::ip_uri(string ip50, string uri50)
{
if(ip50=="74.125.19.1")
{
uri50="www.google.com";
cout<<uri50<<endl;
}
else if(ip50=="74.1.2.3")
{
uri50="www.abc.net";
cout<<uri50<<endl;
}
else
cout<<"nill"<<endl;
};

void ip_uri_store::uri_ip(string uri50, string ip50)
{
if(uri50=="www.google.com")
{
ip50="74.125.19.1";
cout<<ip50<<endl;
}
else if(uri50=="www.abc.net")
{
ip50="74.1.2.3";
cout<<ip50<<endl;
}
else
cout<<"nill"<<endl;
};
int main(int argc, char *argv[])
{
list_head = NULL;
 int i = 0;
   while ( i < argc )
    { work_ptr = list_head ;          // save old head item.
      list_head = new ip_uri_store;  // create new item.
      list_head->next = work_ptr ;    // get next item to point to previous 
      list_head->count = argv[i] ;
      list_head->id = i ;         // fill in payload data.
      i++ ;
    }
 work_ptr = list_head ;          // point to start of list.
   while( work_ptr != NULL)        // not at end.
   { //cout << "   " << work_ptr->id << "  " << work_ptr->count<< endl ;
     work_ptr = work_ptr->next ;  // point to next item in list, or NULL.
    }


char ch;
for (int k = 1; k < 2; ++k) {
    for (int j = 0; argv[k][j] != '\0'; ++j) {
        ch = argv[k][j];
}
}
//cout<<ch<<endl;
if(ch=='I')
{
string uri50;
string ip50=argv[2];
ip_uri_store ipuri;
ipuri.ip_uri(ip50, uri50="");
}
else if(ch=='U')
{
string ip50;
string uri50=argv[2];
ip_uri_store urip;
urip.uri_ip(uri50,ip50="");
}
else 
cout<<"nill"<<endl;

return(0);
}
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.