How to pass a string to a method in a class?
code
class Txtbin{
private:
std::string input;
std::string output = "output.png";
void error();
public:
Txtbin();
void run();
};
Txtbin::Txtbin(){
}
void Txtbin::error(const char* str){
throw std::runtime_error(str);
}
void Txtbin::run(){
if(input == ""){
error("Input file not defined");
}
}
error
# g++ -std=c++11 txtbin.cpp -o txtbin `pkg-config opencv --cflags --libs`
txtbin.cpp:30:6: error: prototype for ‘void Txtbin::error(const char*)’ does not match any in class ‘Txtbin’
void Txtbin::error(const char* str){
^
txtbin.cpp:14:8: error: candidate is: void Txtbin::error()
void error();
^
As others mentioned, you are declaring void error(); but defining void error(const char* str);. Put const char* str parameter in the declaration too, inside the class.
prototype for ‘void Txtbin::error(const char*)’
does not match any in class ‘Txtbin’
You're trying to define Txtbin's void error(const char*) function, but it does not have one.
candidate is: void Txtbin::error()
It does, however, declare a void error() function, without the parameter. Since you actually use that parameter in the implementation, you probably want to add it to its declaration.
Like others have said, void error() requires no parameter. However later you create void error(const char* str) which has a parameter.
class Txtbin{
private:
string input;
string output = "output.png";
public:
Txtbin();
void error(const char*); //This is what you need.
/* void error(); THIS IS WHAT YOU HAD */
void run();
};
void Txtbin::error(const char* str)
{
//Whatever
}
Related
I am a somewhat rusty programmer, and new to C++. I've been asked to write a program that can pass a pointer to a function into another function and execute. I can make the simple case work, where everything is in a .cpp file. But when I place the code in a class inside a .h file it won't compile. I am either code blind or missing something.
Here is the code that works:
/*
* funcptr.cpp
*
* Example:
* - pass function pointer as argument
* - execute passed function
*/
#include <stdio.h>
void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);
// This function gets passed (to takes_a_function)
void print_char(void *data) {
char *ch = (char *)data;
printf("%c\n", *ch);
}
// This function gets passed (to takes_a_function)
void print_int(void *data) {
int *i = (int *)data;
printf("%d\n", *i);
}
void takes_a_function(void (*f)(void *), void *data) {
//f(data); // this also works
(*f)(data);
}
int main() {
int i = 100;
takes_a_function(print_int, &i);
char ch = 'A';
takes_a_function(print_char, &ch);
}
It compiles and runs:
# g++ funcptr.cpp -o funcptr
# ./funcptr
100
A
So far so good. But then I put the code into a .h file and "class"-ify it so I can use it from anywhere, and everything falls apart:
#ifndef __funcptr_h__
#define __funcptr_h__
#include <stdio.h>
void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);
void testit();
class FunctionPtr
{
public:
// This function gets passed (to takes_a_function)
void print_char(void *data) {
char *ch = (char *)data;
printf("%c\n", *ch);
}
// This function gets passed (to takes_a_function)
void print_int(void *data) {
int *i = (int *)data;
printf("%d\n", *i);
}
void takes_a_function(void (*f)(void *a), void *data) {
//f(data); // this also works
(*f)(data);
}
void testit() {
int i = 100;
takes_a_function(print_int, &i);
char ch = 'A';
takes_a_function(print_char, &ch);
}
};
#endif
The compiler error is:
# g++ funcptr.h
funcptr.h: In member function ‘void FunctionPtr::testit()’:
funcptr.h:34:33: error: invalid use of non-static member function ‘void FunctionPtr::print_int(void*)’
takes_a_function(print_int, &i);
^
funcptr.h:22:7: note: declared here
void print_int(void *data) {
^~~~~~~~~
funcptr.h:37:35: error: invalid use of non-static member function ‘void FunctionPtr::print_char(void*)’
takes_a_function(print_char, &ch);
^
funcptr.h:16:7: note: declared here
void print_char(void *data) {
^~~~~~~~~~
I've been playing with this for a while and done a fair amount of reading on passing function pointers, including on StackOverflow, however all the examples I see are simple and of no help.
Any insights are much appreciated.
Thanks to all.
It looks to me like your problem is when you call takes_a_function with your arguments.
You declared the data parameter as a pointer to a void type, not a reference to a void type.
You could try something like:
void testIt() {
int i = 100;
int * j = &i;
takes_a_function(print_int, j);
char c = 'a';
char * d = &c;
takes_a_function(print_char, d);
};
References and pointers are not exactly the same thing.
Also it looks like you forgot a #endif after you define __funtptr_h__
I hope this helped
Error message:
Link to full program
class AddressBook
{
private:
char firstname[20];
char lastname[20];
char no[15];
class adrs
{
public:
char postal[100];
char pincode[7];
friend void say();
friend void Add();
friend void Edit();
friend void View(int);
}address;
char dob[11];
char email[50];
public:
friend void say();
void sort(AddressBook []);
void NumberSort(AddressBook []);
void Add(void);
void Delete(AddressBook [], int pos);
void Edit();
void LinearSearch(AddressBook [], char a[]);
friend void ViewAll();
void View(int);
void FetchContact();
};
This is the declaration of a class for a contact-book program.
void sort(AddressBook []);
void NumberSort(AddressBook []);
void Delete(AddressBook [], int pos);
void LinearSearch(AddressBook [], char a[]);
These lines in the above declaration shows up as an error in TurboC++ compiler. Can anyone tell me why?
In C++ when an array is passed as an argument, its initial address is passed to formal parameter. With the help of this technique the code can be written as follows
void sort(AddressBook*);
void NumberSort(AddressBook*);
void Delete(AddressBook*, int pos);
void LinearSearch(AddressBook*, char a[]);
I have implemented the same thing for my problem and it just worked.
As you were not able to produce minimal example, I did it for you:
class AddressBook
{
void sort(AddressBook[]);
};
This declaration compiles fine on a modern compiler but not on Turbo C++.
You can call this a compiler bug.
There are two options:
you rethink your program and write it another way
you use a modern C++ compiler
Changing school would be another valuable option.
I've created three C++ simple tests in tests folder in my projects directory. When I created the first C++ simple test and built it there was no problem but when I create the second or third, innumerable errors listed below are generated
build/DebugDynamic/GNU-Linux-x86/ClientSocket.o: In function `Socket::is_valid() const':
/home/gowtham/workspace/base/ClientSocket.cpp:8: multiple definition of `ClientSocket::ClientSocket(std::string, int, Socket::SOCKET_TYPE, std::string, std::string, std::string)'
build/DebugDynamic/GNU-Linux-x86/ClientSocket_nomain.o:/home/gowtham/workspace/base/ClientSocket.cpp:8: first defined here
The linker command is
g++ -g -O0 -o build/DebugDynamic/GNU-Linux-x86/tests/TestFiles/f3 build/DebugDynamic/GNU-Linux-x86/tests/tests/sizeInfo.o build/DebugDynamic/GNU-Linux-x86/ClientSocket_nomain.o build/DebugDynamic/GNU-Linux-x86/FFJSON_nomain.o build/DebugDynamic/GNU-Linux-x86/JPEGImage_nomain.o build/DebugDynamic/GNU-Linux-x86/ServerSocket_nomain.o build/DebugDynamic/GNU-Linux-x86/Socket_nomain.o build/DebugDynamic/GNU-Linux-x86/logger_nomain.o build/DebugDynamic/GNU-Linux-x86/myconverters_nomain.o build/DebugDynamic/GNU-Linux-x86/mycurl_nomain.o build/DebugDynamic/GNU-Linux-x86/mystdlib_nomain.o build/DebugDynamic/GNU-Linux-x86/myxml_nomain.o build/DebugDynamic/GNU-Linux-x86/ClientSocket.o build/DebugDynamic/GNU-Linux-x86/FFJSON.o build/DebugDynamic/GNU-Linux-x86/JPEGImage.o build/DebugDynamic/GNU-Linux-x86/ServerSocket.o build/DebugDynamic/GNU-Linux-x86/Socket.o build/DebugDynamic/GNU-Linux-x86/logger.o build/DebugDynamic/GNU-Linux-x86/myconverters.o build/DebugDynamic/GNU-Linux-x86/mycurl.o build/DebugDynamic/GNU-Linux-x86/mystdlib.o build/DebugDynamic/GNU-Linux-x86/myxml.o -lxml2 -lpthread -lssl -lcrypto -lz
netbeans is including a duplicate object file _nomain.o for every object file in the project.
ClientSocket.h
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include "Socket.h"
class ClientSocket : public Socket {
public:
class AftermathObj {
public:
void* (*aftermath)(void* aftermathDS, bool isSuccess);
void* aftermathDS;
std::string payload;
std::string* payloadPTR;
std::string error;
int __flags;
pthread_t t;
ClientSocket* cs;
AftermathObj() {
};
~AftermathObj() {
};
};
ClientSocket();
ClientSocket(std::string host, int port, Socket::SOCKET_TYPE socketType = Socket::DEFAULT, std::string trustedCA = "", std::string privatecert = "", std::string privatekey = "");
std::string host;
int port;
void reconnect();
void disconnect();
bool send(const std::string s, int __flags) const;
bool send(const std::string* s, int __flags) const;
bool send(const std::string s) const;
virtual ~ClientSocket();
const ClientSocket& operator <<(const std::string&) const;
void asyncsend(std::string payload, AftermathObj* after_math_obj);
void asyncsend(std::string* payload, AftermathObj* aftermath_obj);
const ClientSocket& operator >>(std::string&) const;
private:
//Socket* soc;
static void* socsend(void*);
struct soc_send_t_args {
std::string s;
void* (&aftermath)(void* aftermathDS);
void* aftermathDS;
};
pthread_key_t socket_thread_key;
};
#endif
here I have an error but I don't know why it shows. This is the error:
In file included from Exploit.cc:2:0: Command.hh:35:17: error: field
‘_value’ has incomplete type Command.hh: In constructor
‘Command::Command(const char*)’: Command.hh:27:3: error: ‘_value’ was
not declared in this scope make: *** [Exploit.o] Error 1
And this is Command.hh
class Command {
public:
Command(const char* exp){
_value=exp;
_value.append("\n");
}
~Command();
void request(int fd);
void response(std::string res);
const char* getCommand();
private:
std::string _value;
};
Exploit.cc
typedef std::shared_ptr<Command> CommandPtr;
typedef std::list<CommandPtr> CommandQueue;
typedef std::shared_ptr<CommandQueue> CommandQueuePtr;
Exploit::Exploit(const char* exp, int fd2, int type2): fd(fd2), type(type2){
commands_to_execute = make_shared<CommandQueue>();
commands_executed = make_shared<CommandQueue>();
CommandPtr pr=std::make_shared<Command>( exp);
commands_to_execute->push_back(pr);
}
I hope someone could help me, because It's very weird for me.
Thank you!!
Your forgot to include the string header:
#include <string>
in Command.hh.
On a related note, maybe it's a good idea to make the constructor accept an std::string:
Command(const std::string& exp) {
instead of a const char*.
I try to make class where I can creat new object of Student.
I've got some problem with definition of class body (student.cpp) and class (student.h).
Error:
In file included from student.cpp:1:
student.h:21:7: warning: no newline at end of file
student.cpp:6: error: prototype for `Student::Student()' does not match any in class `Student'
student.h:6: error: candidates are: Student::Student(const Student&)
student.h:8: error: Student::Student(char*, char*, char*, char*, int, int, bool)
student.cpp
//body definition
#include "student.h"
#include <iostream>
Student::Student()
{
m_imie = "0";
m_nazwisko = "0";
m_pesel = "0";
m_indeks = "0";
m_wiek = 0;
m_semestr = 0;
m_plec = false;
}
student.h
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{}
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
Your constructor in cpp file does not match constructor in header.
Every constructors/desctructors/methods realizations in cpp should be first defined in class in header.
If you want to have 2 constructors - 1 with no parameters and one with many parameters as you have. You need to add definition of your constructor in header.
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{} //here really implementation made
Student(); //one more constructor without impementation
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
In you header file you declare that Student has just one constructor with all the written parameters but no default Student() constructor, you should add it to header:
class Student {
Student();
Student(char* imie, char* nazwisko ... ) {}
};
You wrote a body for a Student constructor that doesn't take any parameters:
Student::Student( /* NO PARAMETERS */ )
But this function, Student(), is not in the class-definition.
This generates the error:
prototype for `Student::Student()' does not match any in class `Student'
You need to write:
class Student {
public:
Student(); /* NOW it is declared as well as defined */
[... all the other stuff ...]
};
Now, there is both a prototype for Student() and also for Student(/* 7 parameters */)
The fix for the other error is simple:
student.h:21:7: warning: no newline at end of file
The fix is to put a newline at the end of the file! :-)