Here we go, I am working on some code to store an IP address in an object. I have overloaded operators >>, <<, and == and they are working fine with 1 problem. I am supposed to be able to cin >> ip1 >> ip2 >> ip3; but after I input the first IP I get a bus error. If I seperate the cin's(cin >> ip1; cin >> ip2; cin >> ip3;) it works fine.heres the code:

test.cpp

#include <iostream>
//#include "Time.h"          //not using Time class at this moment
#include "IPAddress.h"
using namespace std;
int main()
{
IPAddress ip1, ip2, ip3;
cout << ip1 << ip2 << endl;
cin >> ip1 >> ip2 >> ip3;
//cin >> ip2;
//cin >> ip3;
cout << ip1 << endl;
if(ip1 == ip2) cout << "Same" << endl;
if(ip1 == ip3) cout << "Same" << endl;
return 0;
}

IPAddress.h

#include <iostream>
using namespace std;
class IPAddress
{
friend ostream& operator<< (ostream&, const IPAddress&);
friend istream& operator>> (istream&, IPAddress&);
public:
IPAddress();
int p1, p2, p3, p4;
bool operator==(const IPAddress &) const;
};

IPAddress.cpp

#include <iostream>
#include "IPAddress.h"
#include <string.h>
using namespace std;
IPAddress::IPAddress()
{
p1 = 0;
p2 = 0;
p3 = 0;
p4 = 0;
}
bool IPAddress::operator==(const IPAddress &t2) const
{
if((*this).p1 == t2.p1 && (*this).p2 == t2.p2 && (*this).p3 == t2.p3 && (*this).p4 == t2.p4) return true;
else return false;
}
ostream& operator<< (ostream &out, const IPAddress &r)
{
out << r.p1 << "." << r.p2 << "." << r.p3 << "." << r.p4 << endl;
return out;
}
istream& operator>> (istream &in, IPAddress &r)
{
char mystring[16];
char *str1, *str2, *str3, *str4;
int ptr1, ptr2, ptr3;
in >> mystring;
str1 = strtok(mystring, "."); //tokenize IP string to seperate strings
str2 = strtok(NULL, ".");
str3 = strtok(NULL, ".");
str4 = strtok(NULL, "\0");

r.p1 = atoi(str1); //store IP in object
r.p2 = atoi(str2);
r.p3 = atoi(str3);
r.p4 = atoi(str4);

}

Yeah I'm using c style strings in c++ code get over it :)

Normally I wouldn't bother about something that could be fixed by coding seperate lines instead of putting it all in one, but the instructor specified to use this to test the code. Thanks for the help.

>>Yeah I'm using c style strings in c++ code get over it
that is ok if you have not yet learned about std::string or your instructor requires character arrays.

The function causes buss error probably because it does not return the stream. Your compiler should have given you either an error or a warning about that. Do not overlook warnings because they are often errors.

>>char mystring[16];
too small. you forgot to make room for the null terminator

>>Normally I wouldn't bother about something that could be fixed by coding seperate lines instead of putting it all in one
That would fix nothing because the compiler could care less.

What happens if you type in an ip with incorrect format? strtok() will return NULL at some point and the operator>> method will crash.

Well he said it was ok to use c style strings(which I did because of tokenize function they have). I changed my header file so that p1 - p4 are now c++ strings and got rid of atoi functions. Still taking the input as c style string and tokenizing, then saving to c++ string. I put the return stream in and everything is working fine for calling multiple >> on the same line. I don't need to worry about the incorrect format of IP entered because I will be reading IPs from a log file, but for learning sake I'm gonna look into that anyway. Thanks for the help.

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.