Having problems with classes and chars.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 370 Views - Last Post: 17 July 2012 - 06:02 PM Rate Topic: -----

#1 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Having problems with classes and chars.

Posted 16 July 2012 - 08:08 PM

Hey guys, okay so I am pretty new to programming and I am making an IRC bot.
I am referring to this code written Here for some help and a bit of guidance.

Well when i try and compile my code I get these error messages:

main.cpp: In function ‘int main()’:
main.cpp:8: error: no matching function for call to ‘IrcBot::IrcBot()’
IrcBot.h:7: note: candidates are: IrcBot::IrcBot(char*, char*)
IrcBot.h:5: note:                 IrcBot::IrcBot(const IrcBot&)
main.cpp:9: error: invalid use of ‘class IrcBot’


well here is my code:

main.cpp
#include "IrcBot.h"
#include <iostream>
using namespace std;
int main()
{

IrcBot bot;
bot.IrcBot("NICK gr33nbot\r\n","USER gr33nbot notused notused:Gr33n Bot\r\n");

return 0;
}



IrcBot.h
#ifndef IrcBot_H
#define IrcBot_H

class IrcBot
{
public:
	 IrcBot(char * _nick, char * _user);
	virtual ~IrcBot();

	bool setup;

	void start();
	bool charSeach(char *tosearch, char *searchFor);

private:
const char *port;
int sock;//the socket descriptor

char *nick;
char *usr;

bool isConnected(char *buff);
char * timeNow();
bool sendData(char  *msg);
void sendPong(char *buf);
void msgHandle(char *buf);
};
#endif /* IRCBOT_H */



IrcBot.cpp
#include "IrcBot.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <cstring>

#define MAXDATASIZE 100;

using namespace std;

IrcBot::IrcBot(_nick,_user)
{
nick = _nick;
user = _user;
start();
}
IrcBot::start()
{
struct addrinfo hints, *servinfo;


int sock;
char* port = "6667";
int res;


memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;


if(res = getaddrinfo("irc.freenode.org", port, &hints, &servinfo) != 0)
{
fprintf("Client: getaddrinfo failed:", gai_strerror(res),"\n");
}

if(sock = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol) == -1)
{
cout << "Client: socket failed: " << errno << endl;
}
if(connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) == -1)
{
cout << "Client: connection failed: " << erno << endl;
	
	}
freeaddrinfo(servinfo);


int numbytes;
char *buf;
count = 0;
while(1)
{
switch( count ){
	case:3
		sendData(nick);
		sendData(usr);
		break;
	case:4
		sendData("JOIN ##newboston\r\n");
	default:
		break;
			}
numbytes = recv(sock,buf,MAXDATASIZE -1,0);

	buf[numbytes]="/0";
	cout << buf<< endl;

if(charSearch(buf,"PING")
{
	sendPong(buf);
}

if(numbytes == 0)
		{	

cout << "<-------------------connection failed -------------------------->" << endl;
cout << timeNow << cout;

		}

	}

}
//-----------------------------------------
bool IrcBot::charSearch(char *buf, char *searchFor)
{
char compare[50];
int leng = strlen(buf);
int lengsearch = strlen(searchFor);
bool truefalse;

for(int i = 0; i<leng; i++){

if(leng != lengsearch){
truefalse = false;
goto leave;
}	
if(buf[i]==searchFor[i])
	{
	
	truefalse = true;
	}
else{
cout << "error something wronge happened" << endl;
truefalse = false;
goto leave;
}
leave:;
}
//----------------------------------------
IrcBot::sendPong(const char *buf)
{


char ping[10] = "PING";
char pong[10] = "PONG";
char response[100];

int  len = strlen(buf);
int  count = 0;


for(int z=0;z<len;z++){
response[z] = buf[z];

}

for(int i = 0;i<len;i++)
{
	
	if(response[i]==ping[0])
	{

		for(int x = 0;x<3;x++) 
		{

			if(ping[x]==response[count+x])
			{

				for(int y=0;y<3;y++)
				{
  				response[count] = pong[y];
				count++;	
				} 
				
			
	
			}


		}

	}
count++;

}
int l = strlen(response);
numbytes = send(sock,response,l,0);
return 0;
}

//-------------------------------------------------
char * IrcBot::timeNow()
{
 time_t rawtime;
    struct tm * timeinfo;
 
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
 
    return asctime (timeinfo);

}
//--------------------------------------------------
bool IrcBot::sendData(char *msg)
{
int leng = strlen(msg);
if (send( sock , msg , leng , 0)
{     
return true;
}
else{
return false;
}

}

bool IrcBot::msgHandle(char *msg)
{
if(charSearch(msg,"!help"))
{
sendData("PRIVMSG ##newboston :No commands are working atm\r\n");
}
}



I really am stuck most likely its just a stupid mistake so please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with classes and chars.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 08:16 PM

IrcBot bot;


This invokes the default constructor of IrcBot. Your error message is telling you that IrcBot does not have a default constructor (because you defined a constructor with arguments).

bot.IrcBot("NICK gr33nbot\r\n","USER gr33nbot notused notused:Gr33n Bot\r\n");



You're trying to invoke the constructor as if it was a method. That's not how you invoke a constructor in C++. You need to supply the constructor arguments when you create the variable, not on a later line.

The correct way to invoke the IrcBot constructor with those arguments would be this:

IrcBot bot("NICK gr33nbot\r\n","USER gr33nbot notused notused:Gr33n Bot\r\n");



This creates the variable bot and invokes the IrcBot(char*, char*) constructor with the two strings as arguments.

PS: The type of the strings should really be const char*, not char*. Also you should really consider using std::strings instead of char*s.

This post has been edited by sepp2k: 16 July 2012 - 08:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 08:19 PM

Awesome thanks so much, I will give it a try.
Thanks for fast reply as well :D

This post has been edited by JackOfAllTrades: 17 July 2012 - 03:44 AM
Reason for edit:: Removed unecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 08:54 PM

Okay I did what you said but I am still getting this error
/tmp/ccP0PE0c.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `IrcBot::IrcBot(char const*, char const*)'
main.cpp:(.text+0x2c): undefined reference to `IrcBot::~IrcBot()'
collect2: ld returned 1 exit status


IrcBot.h
#ifndef IrcBot_H
#define IrcBot_H

class IrcBot
{
public:
	 IrcBot(const char *nick,const char *user);
	virtual ~IrcBot();
	
	bool setup;

	void start();
	bool charSeach(char *tosearch, char *searchFor);

private:
const char *port;
int sock;//the socket descriptor

const char *nick;
const char *user;


char * timeNow();
bool sendData(char  *msg);
void sendPong(char *buf);
void msgHandle(char *buf);
};
#endif /* IRCBOT_H */



main.cpp
#include "IrcBot.h"
#include <iostream>
using namespace std;
int main()
{

IrcBot bot("NICK gr33nbot\r\n","USER gr33nbot notused notused: gr33n bot\r\n");
//bot._nick = "NICK gr33nbot\r\n";
//bot._user = "USER gr33nbot notused notused: gr33n bot\r\n";

return 0;
}


I really have no clue what I am doing wrong. DAMM IT!!!
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 08:59 PM

You probably forgot to change the definition of the constructor, so now your definition does not match your declaration.
Was This Post Helpful? 1
  • +
  • -

#6 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:04 PM

main.cpp
#include "IrcBot.h"
#include <iostream>
using namespace std;
int main()
{

IrcBot bot("NICK gr33nbot\r\n","USER gr33nbot notused notused: gr33n bot\r\n");
//bot._nick = "NICK gr33nbot\r\n";
//bot._user = "USER gr33nbot notused notused: gr33n bot\r\n";

return 0;
}


IrcBot.h
#ifndef IrcBot_H
#define IrcBot_H

class IrcBot
{
public:
	 IrcBot(const char *_nick,const char *_user);
	virtual ~IrcBot();
	
	bool setup;

	void start();
	bool charSeach(char *tosearch, char *searchFor);

private:
const char *port;
int sock;//the socket descriptor

const char *nick;
const char *user;


char * timeNow();
bool sendData(char  *msg);
void sendPong(char *buf);
void msgHandle(char *buf);
};
#endif /* IRCBOT_H */


IrcBot.cpp
#include "IrcBot.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <cstring>

#define MAXDATASIZE 100;

using namespace std;

IrcBot::IrcBot(const char* _nick,const char* _user)
{
nick = _nick;
user = _user;
start();
}
IrcBot::~IrcBot()
{
close (sock);

}
//--------------------------------------------
IrcBot::start()
{
struct addrinfo hints, *servinfo;


int sock;
char* port = "6667";
int res;


memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;


if(res = getaddrinfo("irc.freenode.org", port, &hints, &servinfo) != 0)
{
fprintf("Client: getaddrinfo failed:", gai_strerror(res),"\n");
}

if(sock = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol) == -1)
{
cout << "Client: socket failed: " << errno << endl;
}
if(connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) == -1)
{
cout << "Client: connection failed: " << erno << endl;
	
	}
freeaddrinfo(servinfo);


int numbytes;
char *buf;
count = 0;
while(1)
{
switch( count ){
	case:3
		sendData(nick);
		sendData(usr);
		break;
	case:4
		sendData("JOIN ##newboston\r\n");
	default:
		break;
			}
numbytes = recv(sock,buf,MAXDATASIZE -1,0);

	buf[numbytes]="/0";
	cout << buf<< endl;

if(charSearch(buf,"PING")
{
	sendPong(buf);
}

if(numbytes == 0)
		{	

cout << "<-------------------connection failed -------------------------->" << endl;
cout << timeNow << cout;

		}

	}

}
//-----------------------------------------
bool IrcBot::charSearch(char *buf, char *searchFor)
{
char compare[50];
int leng = strlen(buf);
int lengsearch = strlen(searchFor);
bool truefalse;

for(int i = 0; i<leng; i++){

if(leng != lengsearch){
truefalse = false;
goto leave;
}	
if(buf[i]==searchFor[i])
	{
	
	truefalse = true;
	}
else{
cout << "error something wronge happened" << endl;
truefalse = false;
goto leave;
}
leave:;
}
//----------------------------------------
IrcBot::sendPong(const char *buf)
{


char ping[10] = "PING";
char pong[10] = "PONG";
char response[100];

int  len = strlen(buf);
int  count = 0;


for(int z=0;z<len;z++){
response[z] = buf[z];

}

for(int i = 0;i<len;i++)
{
	
	if(response[i]==ping[0])
	{

		for(int x = 0;x<3;x++) 
		{

			if(ping[x]==response[count+x])
			{

				for(int y=0;y<3;y++)
				{
  				response[count] = pong[y];
				count++;	
				} 
				
			
	
			}


		}

	}
count++;

}
int l = strlen(response);
numbytes = send(sock,response,l,0);
return 0;
}

//-------------------------------------------------
char * IrcBot::timeNow()
{
 time_t rawtime;
    struct tm * timeinfo;
 
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
 
    return asctime (timeinfo);

}
//--------------------------------------------------
bool IrcBot::sendData(char *msg)
{
int leng = strlen(msg);
if (send( sock , msg , leng , 0)
{     
return true;
}
else{
return false;
}

}

bool IrcBot::msgHandle(char *msg)
{
if(charSearch(msg,"!help"))
{
sendData("PRIVMSG ##newboston :No commands are working atm\r\n");
}
}


Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:11 PM

Hm, then either IrcBot.cpp was not recompiled after you've changed, or it isn't linked at all.

Edit: Actually after reading the error message again, it says "char const*", not "const char*", so it was probably main.cpp that was not recompiled.

Edit2: Also your IrcBot.cpp contains compilation errors unrelated to the constructor.

This post has been edited by sepp2k: 16 July 2012 - 09:18 PM

Was This Post Helpful? 1
  • +
  • -

#8 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:39 PM

View Postsepp2k, on 16 July 2012 - 09:11 PM, said:

Hm, then either IrcBot.cpp was not recompiled after you've changed, or it isn't linked at all.

Edit: Actually after reading the error message again, it says "char const*", not "const char*", so it was probably main.cpp that was not recompiled.

Edit2: Also your IrcBot.cpp contains compilation errors unrelated to the constructor.

so how do i fix it?
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:45 PM

Try to compile IrcBot.cpp. Notice the errors. Fix the errors (or temporarily remove the methods that cause the errors). Now compile both main.cpp and IrcBot.cpp and link them together.
Was This Post Helpful? 1
  • +
  • -

#10 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:50 PM

View Postsepp2k, on 16 July 2012 - 09:45 PM, said:

Try to compile IrcBot.cpp. Notice the errors. Fix the errors (or temporarily remove the methods that cause the errors). Now compile both main.cpp and IrcBot.cpp and link them together.


Thanks but how do you mean link them together?
also I am sorry i dont know alot ofthis stuff im just starting to learn c++ again and didnt really cover alot of this sort of stuff.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:52 PM

I mean make them into one executable. Do g++ -Whatever main.cpp IrcBot.cpp -o bla or whatever you do in your environment to compile all files in your project into one executable.
Was This Post Helpful? 1
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Having problems with classes and chars.

Posted 16 July 2012 - 09:58 PM

If this is indeed your current code you should have quite a few errors and warnings when you compile your code.

When I compiled your code I got about 20 errors and about 10 warnings.

You also need to find an indentation style you like and use it consistently. This will make your programs much easier to read and troubleshoot. Next stop using goto, learn to use some of the other looping constructs, such as the while, do/while and for loops and proper if/else statements. The use of goto's is a very bad practice and you should stop using this statement now.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 17 July 2012 - 03:23 AM

View Postjimblumberg, on 16 July 2012 - 09:58 PM, said:

If this is indeed your current code you should have quite a few errors and warnings when you compile your code.

When I compiled your code I got about 20 errors and about 10 warnings.

You also need to find an indentation style you like and use it consistently. This will make your programs much easier to read and troubleshoot. Next stop using goto, learn to use some of the other looping constructs, such as the while, do/while and for loops and proper if/else statements. The use of goto's is a very bad practice and you should stop using this statement now.

Jim


Thanks very much Jim I have changed all the chars to string other than the ones needed for the sockets and also i stoped using goto; just a question tho: Can i use the exit; function or is that as bad as goto?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Having problems with classes and chars.

Posted 17 July 2012 - 05:06 AM

In a C program exit() is fine. However in a C++ program you should avoid exit() as it doesn't properly call class destructors. Have your functions return values that can indicate whether the function succeeded to the calling function and have the calling function take appropriate action.

Also don't forget to find the indentation style. This is very important. Your code as presented is very hard to read, making troubleshooting difficult.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 un-codable  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 11

Re: Having problems with classes and chars.

Posted 17 July 2012 - 05:48 PM

View Postjimblumberg, on 17 July 2012 - 05:06 AM, said:

In a C program exit() is fine. However in a C++ program you should avoid exit() as it doesn't properly call class destructors. Have your functions return values that can indicate whether the function succeeded to the calling function and have the calling function take appropriate action.

Also don't forget to find the indentation style. This is very important. Your code as presented is very hard to read, making troubleshooting difficult.

Jim

Do you have any preferences for which indentation style to use
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2