I have window_types.cpp
#include <string>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"
#include "../../utils/utilsf.h"
#include "../../extra_data/extra_data.h"
#include "window.h"
#include "window_types.h"
using namespace std;
using namespace windows;
message_window::message_window(string title,string con,int a[],int b[]) : bwindow(title, 300, 200, 300, 200,a,b){
vector <string> content_ordered;
string new_lines = "";
for (int x = 0;x < con.size();x++){
if (utilsf::cts(con[x]) == "/" and utilsf::cts(con[x]) == "r"){
content_ordered.push_back(new_lines);
new_lines = "";
x+=2;
}
new_lines = new_lines + utilsf::cts(con[x]);
}
SDL_Color textColor = {0, 0, 0};
TTF_Font *font = fonts::load_john_han(15);
int h = 0;
for (int x = 0;x < content_ordered.size();x++){
SDL_Surface* text_surface = TTF_RenderText_Solid(font,content_ordered[x].c_str(),textColor);
apply_surface(5,h,text_surface,content);
h += text_surface->h + 3;
}
}
/*void windows::message_window::start(string title,vector <string> content,int s){
int yp = 200;
int xp = 300;
int w = 100;
int h = 50;
int a[4];
int b[4];
a[0] = utilsf::randrange(0,256,s);
a[1] = utilsf::randrange(0,256,s);
a[2] = 200;
a[3] = 255;
b[0] = 200;
b[1] = utilsf::randrange(0,256,s);
b[2] = utilsf::randrange(0,256,s);
b[3] = 255;
bwindow(title,xp,yp,w,h,a,b);
}*/
void windows::message_window::test(){
}
message_window* get_message_window(string title,string msj,int s){
int a[4];
int b[4];
a[0] = utilsf::randrange(0,256,s);
a[1] = utilsf::randrange(0,256,s);
a[2] = 200;
a[3] = 255;
b[0] = 200;
b[1] = utilsf::randrange(0,256,s);
b[2] = utilsf::randrange(0,256,s);
b[3] = 255;
message_window *w = new message_window(title,msj,a,b);
return w;
}
Here window_types.h
/*
* window_types.h
*
* Created on: Aug 10, 2013
* Author: newtonis
*/
#ifndef WINDOW_TYPES_H_
#define WINDOW_TYPES_H_
#include <string>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"
#include "window.h"
#include "../../extra_data/extra_data.h"
using namespace std;
namespace windows{
class message_window : public bwindow{
private:
vector <string> message;
string title;
public:
message_window(string,string,int a[],int b[]);
void start(string,vector<string>,int);
void test();
};
message_window* get_message_window(string,string,int);
}
#endif /* WINDOW_TYPES_H_ */
And here I am calling the function get_message_window and I am getting the error "undefined reference to `windows::get_message_window(std::string, std::string, int)"
#include <vector>
#include <string>
#include "SDL/SDL.h"
#include "../utils/event.h"
#include "window/window.h"
#include "window/window_types.h"
#include "stage.h"
#include <iostream>
using namespace std;
using namespace windows;
stage::stage(){
int a[4];
a[0] = 100;
a[1] = 150;
a[2] = 200;
a[3] = 255;
int b[4];
b[0] = 245;
b[1] = 218;
b[2] = 129;
b[3] = 255;
add_window( new bwindow("Test window",20,20,200,200,a,b) );
//ERROR HERE!
message_window *w = windows::get_message_window("hello","hello",5);
add_window(w);
}
void stage::graphic_update(SDL_Surface* screen){
for (int x = 0;x < windws.size();x++){
windws[x]->graphic_update(screen);
}
}
void stage::logic_update(events *evs){
for (int x = 0;x < windws.size();x++){
windws[x]->logic_update(evs);
}
}
void stage::add_window(bwindow *win){
cout<<" Window titled "<<win->get_name()<<" added"<<endl;
windws.push_back(win);
}
Apparently, the function declaration in the header (which you haven't shown) puts the function into namespeace windows. But the function definition is in the global namespace. In other words, you've implemented one function, named ::get_message_window, but you are calling a different one, named windows::get_message_window.
Either take the declaration out of a namespace, or put the definition into the namespace.
According to the error message:
message_window* get_message_window(string title,string msj,int s){
should be:
message_window* windows::get_message_window(string title,string msj,int s){
Related
I'm having trouble identifying a possible source of memory corruption in the following code. Is it due to the fact that I don't call the free() function on char *stringToAdd?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 1024
char* add_strings(char *stringOne, char *stringTwo);
int main() {
char s = 'x';
char *stringOne = &s;
char *stringTwo = new char[1025]();
char *final = add_strings(stringOne, stringTwo);
return 0;
}
char* add_strings(char *stringOne, char *stringTwo) {
unsigned int lengthOfStringOne = strlen(stringOne);
unsigned int lengthOfStringTwo = strlen(stringTwo);
cout << lengthOfStringOne << endl;
cout << lengthOfStringTwo << endl;
char *stringToAdd = lengthOfStringOne < lengthOfStringTwo ? stringOne : stringTwo;
unsigned int lengthOfAdd = lengthOfStringOne < lengthOfStringTwo ? lengthOfStringOne : lengthOfStringTwo;
char *final = static_cast<char*>(calloc(MAX, sizeof(char)));
unsigned int avgLengthOfStrings = (lengthOfStringOne + lengthOfStringTwo) / 2;
if (avgLengthOfStrings < MAX) {
strncat(final, stringToAdd, lengthOfAdd);
printf("DONE\n");
} else {
printf("Average length of both input strings exceeds liimit.\n");
free(final);
return NULL;
}
return final;
}
I am a new user of C++ and do not know all about types of variables.
I have this code but it doesn't work normally. For normal i mean - after starting cursor must be random moves for -25 to 25 pixel of screen.
Sorry if i provided few information. Ask me i can send what you want. And sorry for my bad English.
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <playsoundapi.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
// Cursor random moving here :3
int shakecursor() {
POINT p;
int __cdecl GetCursorPos(int &p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 0 -25;
y_p1 = rand() % 0 -25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
int __cdecl SetCursorPos(int x_p1, int y_p1);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```
These
int __cdecl GetCursorPos(int &p);
int __cdecl SetCursorPos(int x_p1, int y_p1);
are not function calls. they are function declarations.
Instead it seems you mean
GetCursorPos( p );
and
SetCursorPos( x_p, y_p );
So i fixed this problem and program doesn't do anything here code:
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <Mmsystem.h>
#include <playsoundapi.h>
#pragma comment (lib, "User32.lib")
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
int shakecursor() {
POINT p;
GetCursorPos(&p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 51 - 25;
y_p1 = rand() % 51 - 25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
SetCursorPos(x_p, y_p);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
//PlaySound(L"start.mp3", NULL, NULL);
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <vector>
#include <sstream>
#define SHMSIZE 1024
using namespace std;
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
struct process
{
int r;
string name;
vector<string> lines;
};
int main(int argc, char * argv[])
{
int firstRun = 1; //Skipping First Line of Assign-1.ip.
int quantum = 0; //For taking input of quantum.
int count = 0; //For number of processes.
int pchtoint;
string c;
char * pch; //For tokenization.
string reading_file; //Reading a line from file.
char * readarr; //Converting "reading_file" to readarr for tokenization.
process * p;
//=== Quantum Input ===//
cout<<"Enter Quantum size [1-1000]: ";
cin>>quantum;
while(quantum < 1 || quantum > 1000)
{
cout<<"Wrong input!!! Enter Again [1-1000]: ";
cin>>quantum;
}
//=====================//
//===Filing===//
ifstream read("Assign-2.ip");
if(read.is_open())
{
while(!read.eof())
{
getline(read, reading_file);
readarr = new char[reading_file.size() + 1];
for(int i = 0; i < reading_file.length(); i++)
{
readarr[i] = reading_file[i];
}
if(firstRun > 1)
{
int countingline = 0; //counting the number of lines in a process.
pch = strtok (readarr," ,");
while (pch != NULL)
{
c = pch[1];
pchtoint = atoi(c.c_str());
p[pchtoint-1].r++;
p[pchtoint-1].lines.push_back(pch);
for(int i = 0; i < p[pchtoint-1].lines.size(); i++)
cout<<p[pchtoint-1].name<<"=="<<p[pchtoint-1].lines.at(i)<<endl;
pch = strtok (NULL, " ,");
}
}
else
{
pch = strtok (readarr,",.-");
while (pch != NULL)
{
count++;
pch = strtok (NULL, ",.-");
}
p = new process[count];
string s = "p";
for(int i = 0; i < count; i++)
{
s = s + patch::to_string(i+1);
p[i].name = s;
s = s[0];
}
firstRun++;
}
}
}
else
{
cout<<"Cannot open file!!!"<<endl;
}
read.close();
return 0;
}
Enter Quantum size [1-1000]: 2
p1==p1-l1
p2==p2-l1
p3==p3-l1
p1==p1-l1
p1==p1-l2
p2==p2-l1
p2==p2-l2
p3==p3-l1
p3==p3-l2
p1==p1-l1
p1==p1-l2
p1==p1-l3
p3==p3-l1
p3==p3-l2
p3==p3-l3
p1==p1-l1
p1==p1-l2
p1==p1-l3
p1==p1-l4
Segmentation fault (core dumped)
I am reading data from a cvs file. and storing it in struct that is p here. but I don't know why it is giving segmentation fault. I am compiling it on ubuntu terminal.
The input file contains data:
P1, P2, P3,
p1-l1, p2-l1, p3-l1
p1-l2, p2-l2, p3-l2
p1-l3, , p3-l3
p1-l4, ,
Getting Type expression list treated as compound expression in initializer
On both these function calls -
char itoa(new_total, new_total_ch, 10);
int setenv("COUNT_TOTAL", new_total_ch, 1);
Here's the code snippet -
#include <iostream>
// create process team
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctime>
// initialize & process
#include <time.h>
#include <string>
#include <fstream>
#include <cmath>
#include <iosfwd>
// initialize & process
using std::ifstream;
using namespace std;
class Count3sProcessParallel {
public:
//int process();
int pr_count;
int process();
typedef int COUNT_TOTAL;
private:
int worker;
//declare process()
long unit_of_work;
long lower_bound;
long upper_bound;
int pr_i;
char * ct;
int ct_i;
int new_total;
char itoa();
char * new_total_ch;
int setenv();
};
int Count3sProcessParallel::process() {
// determine upper lower bounds
unit_of_work = in_length/workers_num;
lower_bound = (worker -1) * unit_of_work;
upper_bound = (worker * unit_of_work) -1;
// iterate and count
pr_count = 0;
for (pr_i = lower_bound; pr_i < upper_bound; pr_i++)
if (floor(in_buffer[pr_i] == 3))
pr_count++;
return pr_count;
//update COUNT_TOTAL
ct = getenv("COUNT_TOTAL");
ct_i = atoi(ct);
new_total = (pr_count + ct_i);
char * new_total_ch[33];
char itoa(new_total, new_total_ch, 10);
int setenv("COUNT_TOTAL", new_total_ch, 1);
delete[] in_buffer;
return 0;
}
How do I resolve this? Thanks.
Remove the leading types. Just use:
itoa(new_total, new_total_ch, 10);
setenv("COUNT_TOTAL", new_total_ch, 1);
I want to create some modules for my program. I want to call a function and pass a vector as a parameter. The return value should also be a vector.
My code looks like this
main.cpp
//BlueSmart.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
using namespace std;
#pragma comment(lib, "Irprops.lib")
BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};
BLUETOOTH_RADIO_INFO m_bt_info = {
sizeof(BLUETOOTH_RADIO_INFO),
0,
};
BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL
};
BLUETOOTH_DEVICE_INFO m_device_info = {
sizeof(BLUETOOTH_DEVICE_INFO),
0,
};
HANDLE m_radio = NULL;
HBLUETOOTH_RADIO_FIND m_bt = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
int wmain(int argc, wchar_t **args) {
while(true) {
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
do {
localBluetoothDevices ();
m_search_params.hRadio = m_radio;
::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
vector<wstring> vec;
int m_device_id = 0;
do {
wostringstream tmp;
++m_device_id;
//Something like this <----------------------------------------
externBluetoothDevices (vec);
//Something like this <----------------------------------------
wprintf(L"********************************************************************** \n");
wprintf(L"\tDevice %d:\r\n", m_device_id);
wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
wprintf(L"====================================================================== \n");
for (int i = 0; i < 6; i++) {
tmp << hex << m_device_info.Address.rgBytes [i];
if (i < 5)
tmp << L':';
}
vec.push_back(tmp.str());
} while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
BluetoothFindDeviceClose(m_bt_dev);
//Sleep(10*1000*60);
Sleep(10000);
} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
BluetoothFindRadioClose(m_bt);
}
return 0;
}
//Lokal verfügbare bzw. angeschlossene Bluetooth-Devices
void localBluetoothDevices (){
int m_radio_id = 0;
m_radio_id++;
BluetoothGetRadioInfo(m_radio, &m_bt_info);
//Lokaler Bluetoothadapter
wprintf(L"====================================================================== \n");
wprintf(L"Local Device Nr. %d\n", m_radio_id);
wprintf(L"\tName: %s\r\n", m_bt_info.szName);
wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);
}
//Extern verfügbare bzw. Bluetooth-Devices
vector<wstring> externBluetoothDevices (vector<wstring> &vec){
return vec;
}
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <bthdef.h>
#include <BluetoothAPIs.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
#include <conio.h>
void localBluetoothDevices ();
vector<wstring> externBluetoothDevices (vector<wstring>);
It says that vector is not a known type. What am I doing wrong?
In stdafx.h replace
vector<wstring> externBluetoothDevices (vector<wstring>);
with
std::vector<std::wstring> externBluetoothDevices (std::vector<std::wstring>);
Basically the issue was although you put using namespace std; in your cpp file that doesn't count in your header file which is before the using declaration is seen.
Also note that your defintion in the cpp file is different. In the cpp file you have a reference
vector<wstring> externBluetoothDevices (vector<wstring>&);
Decide which you really want.
You should pass a pointer of a vector.