I have the following code and i am programming in c++ :-
I followed the instructions given here by the members and changed the code as :-
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <wx/thread.h>
#include <wx/log.h>
#include <wx/app.h>
using namespace std;
class MyThread;
class MyThread : public wxThread {
public:
MyThread(unsigned int& c);
virtual ~MyThread();
wxThreadError Create(unsigned int stackSize = 0);
wxThreadError Run();
wxThreadError Delete(ExitCode* rc = NULL, wxThreadWait waitMode =
wxTHREAD_WAIT_BLOCK);
virtual ExitCode Entry();
private:
unsigned int& counter;
};
MyThread::MyThread(unsigned int& c)
{
counter = c;
}
MyThread::~MyThread()
{
}
wxThread::ExitCode MyThread::Entry()
{
while(counter < 0xFFFFFFFF)
++counter;
return 0;
}
int main(int argc, char** argv) {
unsigned int uiCounter = 0;
MyThread *mt = new MyThread(unsigned int&);
if (mt) {
if (mt->MyThread::Create() == wxTHREAD_NO_ERROR) {
if (mt->MyThread::Run() == wxTHREAD_NO_ERROR) {
}
}
mt->Delete();
}
char cChar = ' ';
while (cChar != 'q') {
cout << uiCounter<< endl;
cChar = (char) getchar();
}
return 0;
}
And have faced the dollowing errors now
newmain.cpp: In constructor 'MyThread::MyThread(unsigned int&)':
newmain.cpp:38:1: error: uninitialized reference member 'MyThread::counter' [-
fpermissive]
newmain.cpp: In function 'int main(int, char**)':
newmain.cpp:57:33: error: expected primary-expression before 'unsigned'
In this problem what i am doing is that i have got two threads one is the main() and other one i have derived from wxthread
The compiler messages tell you how to remove the errors. Start at the top and tackle them one by one.
newmain.cpp:23:25: error: 'Entry' declared as a 'virtual' field
so the error is in this line
virtual void *Entry(LPVOID param);
Take a look at the definition of wxThread::Entry() which can be found here http://docs.wxwidgets.org/2.8/wx_wxthread.html#wxthreadentry
You will see immediately that the base method has a return value and no parameters. The function you create to override the base method must do the same.
Related
I have looked at other forum posts and I am still confused. I am very new to coding so a simple answer would be appreciated. I am trying to create a simple program that uses sets and gets to set the player's attributes and then gets them with functions. However, whenever I call the functions I get the error.
Here is my .h file:
#pragma once
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
using namespace std;
class Player
{
private:
string name;
int health;
int strength;
int stamina;
int experience;
bool passive;
public:
string GetName();
string SetName(string tName);
int GetHealth();
int SetHealth(int tHealth);
int GetStrength();
int SetStrength(int tStrength);
int GetStamina();
int SetStamina(int tStamina);
int GetExperience();
int SetExperience(int tExperience);
bool GetPassive();
bool SetPassive(bool tPassive);
};
And here is my 1st then 2nd cpp file:
#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\\Users\\Ryan Bell\\Desktop\\School\\2nd Year\\Quarter 1\\Programming\\Week 1\\PlayerClass\\PlayerClass\\PlayerClass.h"
Player::Player()
{
name = "";
health = 100;
strength = 30;
stamina = 100;
experience = 20;
passive = false;
}
string Player::GetName()
{
return name;
}
string Player::SetName(string tName)
{
name = tName;
return "Ok";
}
int Player::GetHealth()
{
return health;
}
int Player::SetHealth(int tHealth)
{
health = tHealth;
}
int Player::GetStrength()
{
return strength;
}
int Player::SetStrength(int tStrength)
{
strength = tStrength;
}
int Player::GetStamina()
{
return stamina;
}
int Player::SetStamina(int tStamina)
{
stamina = tStamina;
}
int Player::GetExperience()
{
return experience;
}
int Player::SetExperience(int tExperience)
{
experience = tExperience;
}
bool Player::GetPassive()
{
return passive;
}
bool Player::SetPassive(bool tPassive)
{
passive = tPassive;
}
#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\\Users\\Ryan Bell\\Desktop\\School\\2nd Year\\Quarter 1\\Programming\\Week 1\\PlayerClass\\PlayerClass\\PlayerClass.h"
int main()
{
Player Player1;
Player1.SetName("Jake");
Player1.SetHealth(100);
Player1.SetStrength(30);
Player1.SetStamina(50);
Player1.SetExperience(0);
Player1.SetPassive(true);
cout << "Player " << Player1.GetName() << ".";
}
Thank you for your time and help!
I don't see a Player Constructor declared:
// I see the Player Constructor definition here.
Player::Player()
{
.....
But this method is not declared in the class.
class Player
{
private:
.....
public:
Player(); // Add this line.
string GetName();
PS. Your code should compile and "probably" runs but is not good. If you can show it working, you can take it to Code Review and get them to give you advice on how to make it better (and some obvious errors corrected).
The compilation of your code gives:
2.cpp: In member function ‘int Player::SetStamina(int)’:
2.cpp:18:1: warning: no return statement in function returning non-void [-Wreturn-type]
18 | }
| ^
2.cpp: In member function ‘int Player::SetExperience(int)’:
2.cpp:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
28 | }
| ^
2.cpp: In member function ‘bool Player::SetPassive(bool)’:
2.cpp:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
38 | }
These can be fixed by turning all setters as functions returning void, e.g:
void Player::SetStamina(int tStamina)
Do it both in *.h and player.cpp.
After fixing this, we bump onto linker errors:
/usr/bin/ld: /tmp/ccQk0cvm.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `Player::SetName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0xa7): undefined reference to `Player::SetHealth(int)'
/usr/bin/ld: main.cpp:(.text+0xb8): undefined reference to `Player::SetStrength(int)'
/usr/bin/ld: main.cpp:(.text+0x11a): undefined reference to `Player::GetName[abi:cxx11]()'
collect2: error: ld returned 1 exit status
This means that you forgot to define Player::GetName, Player::SetStrength(int), and 2 other methods.
This leads us to the simplest solution (for now): modify the public section of your class so that it implements all functions:
public:
std::string GetName() const { return name; }
void SetName(string tName) { name = tName; }
int GetHealth() const { return health; }
void SetHealth(int tHealth) { health = tHealth; }
int GetStrength() const { return strength; }
void SetStrength(int tStrength) { strength = tStrength; }
int GetStamina() const { return stamina; }
void SetStamina(int tStamina) { stamina = tStamina; }
int GetExperience() const { return experience; }
void SetExperience(int tExperience) { experience = tExperience; }
bool GetPassive() const { return passive; }
void SetPassive(bool tPassive) { passive = tPassive; }
and don't use a separate source file for the class member definitions.
Alternatively, you can keep the file, but then you must add all missing function definitions in it.
Notice that I declared all getters as const member functions. Believe me, this is how they should be defined.
The code should explain my difficulty. Though the code itself is quite meaningless, I'm planning to add containers in MyClass, and use algorithms with member functions.
#include <cstdlib>
#include <algorithm>
#include <functional>
using namespace std;
class MyClass
{
public:
MyClass() { a = 0; }
~MyClass() {}
private:
int a;
bool tiny_test (int);
int Func();
};
bool MyClass::tiny_test (int b)
{
return a == b;
}
int MyClass::Func()
{
// does not compile
(mem_fun(&MyClass::tiny_test))(this);
// commented below is another attempt, also no success
//mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test);
//tmp_functor(this);
return 0;
}
int main(int argc, char** argv)
{
return 0;
}
Thanks a lot! Btw, I'm not using a static member function, simply because I believe it must work for non-static member functions.
P.S. Eric, Jarod42, thanks for prompt replies!
bool MyClass::tiny_test (int b)
{ // ^^^^^ You missed this argument
return a == b;
}
Try this:
// Supply one more argument. E.g., 3
(mem_fun(&MyClass::tiny_test))(this, 3);
How can I call the thread_ready_function into a thread as commented, using pthread ? I need to call it with the class object (In the real world the function uses attributes previously set).
MWE
#include <iostream>
#include <pthread.h>
class ClassA
{
public:
void * thread_ready_function(void *arg)
{
std::cout<<"From the thread"<<std::endl;
pthread_exit((void*)NULL);
}
};
class ClassB
{
ClassA *my_A_object;
public:
void test(){
my_A_object = new ClassA();
my_A_object->thread_ready_function(NULL);
// my_A_object->thread_ready_function(NULL);
// ^
// I want to make that call into a thread.
/* Thread */
/*
pthread_t th;
void * th_rtn_val;
pthread_create(&th, NULL, my_A_object.thread_ready_function, NULL);
pthread_join(th, &th_rtn_val);
*/
}
};
int main()
{
ClassB *my_B_object = new ClassB();
my_B_object->test();
return 0;
}
if you don't want to use C++11 or stl or boost, you must use the static key word for your member function,so that the pthread can call your member function!
example code:
#include <iostream>
#include <pthread.h>
using namespace std;
class A{
public:
static void* thread(void* args);
int parella_thread(int thread_num);
};
void* A::thread(void* args)
{
cout<<"hello world"<<endl;
}
int A::parella_thread(int thread_num)
{
pthread_t* thread_ids = new pthread_t[thread_num];
for(int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,thread,(void*)NULL);
}
delete[] thread_ids;
}
int main(int argc,char*argv[])
{
A test;
test.parella_thread(4);
return 0;
}
I'm having a problem with SDL threads so I made a little multi file code so it will be easier to show my problem
Header file
#ifndef MAINC_H_INCLUDED
#define MAINC_H_INCLUDED
#include <iostream>
#include <CONIO.H>
#include <SDL.h>
#include <SDL_thread.h>
using namespace std;
class mainc
{
private:
SDL_Thread* thread;
int threadR;
int testN=10;
public:
int threadF(void *ptr);
int OnExecute();
bool start();
};
#endif
One file
#include "mainc.h"
bool mainc::start() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
getch();
if(SDL_CreateThread(threadF, "TestThread", (void *)NULL)==NULL){
return false;
}
return true;
}
int mainc::threadF(void *ptr){
cout<<"This is a thread and here is a number: "<<testN<<endl;
return testN;
}
Second file
#include "mainc.h"
int mainc::OnExecute() {
if(!start())
{
return -1;
}
SDL_WaitThread(thread,&threadR);
return 0;
}
int main(int argc, char* argv[]) {
mainc game;
return game.OnExecute();
}
When I compile this I get this error
cannot convert 'mainc::threadF' from type 'int (mainc::)(void*)' to type 'SDL_ThreadFunction {aka int (*)(void*)}'
I dug around a bit and I found a solution but it gave me other errors I needed to make threadF static but I couldn't access any variables it gave me this error
invalid use of member 'mainc::testN' in static member function
But if I remove the variable from the function it runs fine
Now I don't know what do to because in my game I need to share variables which change
testN is neither a static nor a public property of class mainc and to do what you're trying to do, it needs to be either.
If you want to use members of class "mainc" from within another thread body, you need to pass a pointer to an object of class "mainc" to SDL_CreateThread:
// ...
SDL_CreateThread(threadF, "TestThread", this)
// ...
and then
int mainc::threadF(void *ptr)
{
mainc* myMainc = (mainc*)ptr;
myMainc->testN; // now you can use it as you need
}
Beware of the encapsulation, though (testN is actually private)
g++ gives a re-declaration error when using previously declared variable in anonymous instance creation.
I have the following code in my "weird.cpp" source file:
#include <iostream>
int main()
{
int i = 0;
int j = int ( i );
int ( i );
}
The error i am getting is,
weird.cpp: In function ‘int main()’:
weird.cpp:7: error: redeclaration of ‘int i’
weird.cpp:5: error: ‘int i’ previously declared here
I have tried this in mac and linux with versions 4.2 and 4.7 respectively. I have also tried with other types instead of int. The result is the same error. Can anyone help me understand this problem? Thanks.
First of all, the parentheses you're using here don't do anything.
int i = 0;
int j = int(i); // This is casting i to an int. It's already an int.
int j = i; // This does the same as the last line.
int (i); // This is redeclaring an int named i, which had already been done.
int i; // This is the same as the last line.
What you are saying about an object accepting an int in it's constructor doesn't make sense.
struct A { A(int) {} };
int i;
A obj(i); // A instance named obj which takes integer i as constructor argument.
I don't really understand what you're trying to achieve here, perhaps this?
int i = 0;
int j = i;
{
int i; // In another scope, shadowing the first i for scope duration.
}
You could be forgiven for being confused by this, it's a case of C++'s context-sensitive nature and how that is interpreted by the compiler.
int (i);
is being treated as a declaration of "i" (and since you already have a variable called i in this scope and have not enabled -Wno-shadow, it's not allowing this).
Contrast with the following case, which doesn't compile: (see http://ideone.com/QuwnTC)
#include <iostream>
class Bark {
public:
Bark(const char* msg, const char*) {
std::cout << "Hear ye, hear ye. " << msg << std::endl;
}
};
void bark(const char* i) {
Bark (i); // error here.
}
int main(int argc, const char* argv) {
bark("wtf");
}
It complains that Bark (i) shadows "i"s declaration.
However, both of the following DO compile: http://ideone.com/dcGMET
void bark(const char* i) {
Bark (i + 1);
}
or having two arguments inside the parenthesis: (http://ideone.com/tMzSY9)
#include <iostream>
class Bark {
public:
Bark(const char* msg, const char*) {
std::cout << "Hear ye, hear ye. " << msg << std::endl;
}
};
void bark(const char* i) {
Bark (i, NULL);
}
int main(int argc, const char* argv) {
bark("wtf");
}
Clearly, the treatment of "type (name)" here is some sort of special case, and you might want to raise this with the compiler developers.