C++ :: How To Pass Variable To A Function In Constructor
Jun 11, 2014
In a project I am working on, I have to initialize a window and pass it as a parameter to another constructor, but before the window is initialized, it is passed as a parameter thus causing an error. Here is some code :
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mWorld(mWindow) //<---- right here is where the mWindow variable needs to be passed
{
//...
}
Is there a way to make this work???
View 2 Replies
ADVERTISEMENT
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Feb 24, 2012
I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage. Here's a simplified version of what I'm working on:
#include <iostream>
class board {
public:
int state[64];
board(int arr[])
[Code] ....
Why this doesn't work and how to properly pass an array? Notes: I don't want to copy the array. Also, I'm using MSVC++ 2010 Express.
View 1 Replies
View Related
Jan 30, 2013
I'm trying to pass an reference of the object ifstream to my constructor as such:
// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}
I am defining the constructor as follows, in myClass.cpp:
myClass::myClass(ifstream &file){
// do stuff
}
I get the error of "No overloaded function of myClass::myClass matches the specified type."
Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"
Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>
What am I doing wrong? How can I pass an ifstream reference to my constructor?
View 3 Replies
View Related
Feb 20, 2013
I am having trouble finding the solution to printing the contents of a variable. Here is my code:
void OnSize(HWND hwnd, UINT flag, int width, int height) {
std::wstringstream ss (std::wstringstream::in | std::wstringstream::out);
ss << flag;
std::wstring myStr = ss.str();
MessageBoxW(hwnd,myStr, L"Window Resized",NULL);
}
Note, I have #defined UNICODE at the beginning. The compiler tells me this for myStr.
std::wstring myStr
Error: No suitible conversion function from "std::wstring" to "LPCWSTR" exits
View 2 Replies
View Related
Jun 7, 2014
The instructions: A nutritionist who works for a fitness club facilitate members by evaluating their diets.As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consume in a day. Then, she calculates the number of calories that results from the fat using the following formula:
Calories from fat = fat grams x 9
Next, she calculates the number of calories that result from the carbohydrates using the following formula:
Calories from Carbs = Carbs grams x 4
Create an application that will make these calculations
-DO NOT use global variables
-Create two variables in main that will hold the two results
-Pass fat and carbs by value, and the result variables by reference
-Output in main
The key with what you need to pass is this: Pass fat and carbs by value, and the result variables by reference. This is what I have to far but I don't understand how to "pass by value, and results by variable" ....
#include <iostream>
using namespace std;
void grams (int);
void calories ();
int main () {
//Get fat and carb grams
[Code] ....
View 2 Replies
View Related
May 14, 2012
I am doing a piece of gui code on some embedded system.
I'm trying to find a way of eliminating the local variable kEvent:
Code:
EVENT kEvent;
....
Code:
kEvent = EVENT_UPSTREAM;
xStatus = xQueueSendToBack(getEventProcessorQueue(),&kEvent, 0 );
....
I tried this, it doesn't work:
Code:
xStatus = xQueueSendToBack(getEventProcessorQueue(),(EVENT *)EVENT_UPSTREAM, 0 );
Shouldn't this work?
View 1 Replies
View Related
Apr 12, 2013
I am trying to generate a couple of vectors, but the exact number of vectors that will be needed can only be determined at runtime. Therefore I had the idea to use a macro call and text substitution in order to declare the necessary number of vectors. Unfortunately, the arguments of a macro call are always (as far as I know) considered text only. So, the loop in my example below in which I am trying to generate the following code:
vector<int> vector0;
vector<int> vector1;
vector<int> vector2;
vector<int> vector3;
does not work.
#include <iostream>
#include <vector>
using namespace std;
#define declareVec(vecno) vector<int> vector##vecno;
[Code]......
I get an an error message 'vector0' was not declared in this scope.
I also tried to initialize a string or char with the content "0" and then pass this string or char to the macro call, but this did not work either.
Is there any way in which I could use the content of an integer variable or any other variable as arguments for a macro call?
View 1 Replies
View Related
Jan 23, 2015
I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?
I want to do something like this, but I know this isn't allowed:
/* class.h */
class foo {
public:
foo(*parentWindowPtr);
[Code] .....
I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.
View 4 Replies
View Related
Feb 8, 2013
at my work we use a static analysis tool and it is pointing out some uninitialized variables. One of which is a class member variable which is a C struct. Now, that variable is already declared in the header file for the class.
Currently in the class constructor I am doing:
Code:
memset( &thestruct, 0, sizeof( thestruct ) );
Is there a C++ way to do this? I Googled this but all I really found was:
Code:
StructDef thestruct = {};
Which doesn't really apply here.
View 7 Replies
View Related
Dec 7, 2013
For my default constructor I need to set an array of strings to null and the size variable to 0. Here is my code for that
DynamicStringArray::DynamicStringArray() {
*DynamicArray[]=NULL;
size=0;
}
When I try and compile it I get an "expected primary expression before ']' token" error.
View 1 Replies
View Related
Feb 12, 2014
I'm trying to pass 2 arrays into a void funtion, and return values to one function.
this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.
#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };
[code]...
The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
View 6 Replies
View Related
Jun 27, 2014
I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)
My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.
Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file
[Code] ....
View 1 Replies
View Related
Dec 26, 2014
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')
[Code] .....
View 3 Replies
View Related
Mar 30, 2014
In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.
ReadNew function reads the file....check to see if it exist
CreateNew function creates a new file.
In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions
Here is my code.
Code:
//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
[Code]...
View 3 Replies
View Related
Sep 12, 2013
How can I pass the name from the user input to the constructor ?
Code:
#include <iostream>
#include <string>
using namespace std;
class myClass {
[Code]....
View 6 Replies
View Related
Mar 15, 2013
I'm trying to pass an array from one function to another without resorting to pointers and I have to use Pass by Reference. I can't seem to figure it out. I've been looking at many examples and just don't get it.
the function createAbc makes an array populated with the Alphabet. I need to pass that into my display function so that i can work with the array there. I don't know how to pass it in there... :(
#include "stdafx.h"
#include <iostream>
#include <cctype>
[Code]...
View 9 Replies
View Related
Apr 24, 2014
Why the value does not return; Here is code
#include <iostream>
#include <string>
#include <ctime> // to use the time function
#include <cstdlib>
using namespace std;
int getUserChoose (int);
[Code] ....
here is the output
Welcome to the program of Rock, Paper, Scissors
The computer is ready to play the game
Are you ready to play the game
Y for yes and N for no
Y
R = Rock; P = Paper; S = Scissors
R
You have choose Rock
1TN
1RM
0U
0C
View 5 Replies
View Related
Dec 3, 2014
I am writing a driver program that will eventually need to pass two strings to a function I am writing in a separate file. I am reading data from a file that is formatted like this:
ac: and
amo: love
amor: love
[Code].....
Only a few of the words are actually being processed in the way I want. It appears that only words followed by a comma are processed correctly.
View 1 Replies
View Related
Oct 15, 2013
I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like
struct item {
int ID;
int special; // for function
};
item Key;
Key.special = UseKey(KEY_KING);
// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this
if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;
View 5 Replies
View Related
Apr 12, 2013
i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.
main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"
[Code].....
View 7 Replies
View Related
Aug 23, 2013
How to do i fix this code im not good at using function, im trying to pass a statement from switch to my double passengersR i always come out with a error
this output:
Name passenger:
Enter number passenger:
Choose from A to D
if choose A
Total Regular Passenger: 6.19624e-312
did i do something wrong im not really good at function and im trying to learn how to pass switch to void function
Code:
#include<conio.h>
#include <iostream>
#include <Iomanip>
#include <string>
using namespace std;
double passengersR ()
{ double m;
[Code]...
View 4 Replies
View Related
Aug 4, 2013
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){
[Code]...
View 3 Replies
View Related
Jan 2, 2014
I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like
char* charptr;
and then calling
myfunct(charptr);
and then inside the function i would set charptr equal to another char ptr, simply like
charptr = anothercharptr;
But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.
I solved this issue by calling the function like
myfunct(&charptr);
and declaring the function like
myfunct(char**);
and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;
It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.
What is the difference between the value of the char* charptr written as either charptr and &charptr?
View 1 Replies
View Related
Jun 1, 2013
How to pass the char something[8][8] for example to function void ( char pass.....)???
View 7 Replies
View Related
May 2, 2013
I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1
The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
[Code]....
View 1 Replies
View Related