C/C++ :: How To Make A Function Prototype That Uses Variables By Reference
Dec 4, 2014
How to how to make a function prototype that uses variables by reference. I'm making a decision based game where two running totals of two variables (ending and morality from decisions made) will decide the game outcome. I only have a few modules put in so far and most of the "story" parts cut down here to save space. I'm also getting an error saying there is more than one instance of overloaded function for the "whatToDo" module.
// ZombieGame.cpp : Defines the entry point for the console application.
//
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <iostream>
using namespace std;
//These are function prototypes to declare the functions being used
void WakeyWakey();
void TwentyMinsLater(int);
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int doSomething(int &x, int &y) { int temp =x; x = y * 10; y = temp * 10; return x + y; }
I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"
What am I doing incorrectly?
#include <iostream> using namespace std;
int doSomething(int*, int*);
int main(){ int X, Y, result;
[Code] ....
I have multiplied both x and y by 10 and then added them together!
Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.
<< result << ". "; system("PAUSE"); return 0; } int doSomthing(int *x, int *y)
So I need to make a main function have no if/for/etc. statements so I need to move it to another function and call it in main. The problem is that it's a command line argument function so I'm confused on how it works. Here's an example:
Code:
#include <stdio.h> int main(int argc, char* argv[]) { printf("The program name %s", argv[0]); if (argc == 2) { printf("Argument supplied is %s", argv[1]); } else if (argc > 2) { printf("Too many arguments");} else { printf("One argument");} }
How can i make this into two functions with main only declaring variables and calling other functions?
The function im having problems with takes an array where each element is an array of unsigned chars i.e. octals representing a bitmap of one of 95 ASCII code characters and searches through this two dimensional array looking for a match for a predetermined of array of unsigned chars i.e. the bitmap of a predetermined char. If we find the char the function outputs the index in the two-dimensional array where each elem. is an array of octals ELSE it return -1 i.e. when the char is not found.
I have 2 files, one .cpp, the other .h. There is a function named find_char. See INPUT and OUTPUT on line 48 in .cpp file.
The exception im getting is: font2.cpp:23:45:error: invalid conversion from unsigned char to unsigned char(*)[5]
The input type specified for my function prototype corresponding to find_char. If I put just unsigned char it doesn't fix the problem because it's an array parameter i.e. like a call by reference. I've lead myself to believe that the array variable contains a pointer to the first value in the array and so I've made function prototypes that work with a T* i.e. a pointer to type T. Making the function prototype argument unsigned char* i.e. a pointer to unsigned char simply gives me the exception: "invalid conversion from unsigned char to unsigned char*". When I have the argument be 'unsigned char' I get undefined reference to find_char(unsigned char). URL.....
#include<iostream> #include<string> using namespace std; int bin2dec(const string binarystring); // Declaration of bin2dec prototype with one string parameter int main() { cout << "Enter Q to terminate program.
[Code] .....
Right now the code is in the form of a 'const string binarystring' and were supposed to be able to use 'const char binarystring[]' and for some reason whenever i try to switch it i run into problems when referencing the main function to the int bin2dec(...) function.
I wanted to know if theres a simple way to switch the prototype to an array type of function with [] without changing the entire code.
#include <ctime> #include <iostream> #include <sstream> #include <string> #include <fstream> class Debugger; class Debugger {
[Code] ....
My question is regarding the GetCurrentDebugLevel function above. Ideally I would like to use just one function to get the current debug level of the class, the std::string form would be used to save the "debugging level" in a settings file and the enumerated version would for evaluation purposes throughout the program.
My question is if implementing function prototypes by return value the best way to go or should I do something else?
#include<iostream.h> #include<conio.h> int main() { setcolor(BLUE); setbkcolor(yellow); cout<<"the text in blue colour with yelow background"; getch(); return 0; }
it keeps returning the same error always in my turbo c++ compiler . What is the correct program for changing background colour and text colour ...
Variably modified types are subject to certain restrictions , just as variable-length arrays are. The most important restriction is that the declaration of a variably modified type must be inside the body of a function or in a function prototype.
Code: void f(int m , int n) { int a[m][n] , (*p)[n]; p=a; //.... }
What does it mean? That we can't declare such a pointer as a global variable?
I am trying to computed the time it takes for a projectile to hit the ground. The problem is that i need to square the input of velocity before i do the calculation. the question I have is that if it's possible to have multiple arguments inside the brackets after main.
#include <stdio.h> #include <math.h> double distance (double a, double v, double g); int square(int y); double height(double v, double a, double g); double time (double v, double a, double g); double sqrt(double num);
1. I cannot find a header file to #include that has the sleep function prototype.
2. When I add my own sleep function prototype, I get an unresolved external reference error (for _sleep, not sleep).
What must I #include to get the sleep function prototype? What lib must I include in the linker configuration to resolve the external reference? (I suspect that if I #include the correct header file, the second question might become moot.)
The "man page" at [URL] .... says the header file is <WinBase.h>. But #include'g only <WinBase.h> results in compilation errors.
A response marked "answer" at [URL] ..... says <windows.h> [sic]. #Include'g only <Windows.h> does eliminate the compilation errors.
But apparently that does not bring in the sleep function prototype. Neither does also subsequently #include'g <WinBase.h>. (Which seems to be #include'd by <Windows.h> anyway.)
But even with my own function prototype shown below, I get an unresolved external reference for _sleep. Is that a symptom of my problem: my sleep reference is changed to _sleep? If so, how can avoid that?
According to "man page" (see link above), the external should be resolve in kernel32.lib. And kernel32.lib does appear in the "Additional Dependencies" list under Configuration Properties Linker Input.
Since I am not using C++ features, I tried setting "Compile as C" under Configuration Properties C/C++ Advanced, to no avail.
My code....
#include "stdafx.h" #include <stdlib.h> #include <time.h> #include <Windows.h> void sleep(DWORD msec); // added later int _tmain(int argc, char* argv[])
I am quite new to C programming. Now facing lots of problem with the code below. I attempt to convert he alphabet sorting into function prototype model but still facing warning during compilation.
Besides, I wish to open multiple file at the same time as well using array looping method, but got no idea to modify it...
For whatever reason, I get an error meassage about lines 53-57 saying there is no matching function to call to. Yet the header and the prototype are correct (I think anyways).
#include <iostream> #include <string> #include <fstream> #define N 10 using namespace std; class cust{
Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).
#include <string> using namespace std; class StudentTestScores{ private: string studentName; float *testScores; // used to point to an array of test scores int numTestScores; // number of test scores
so in declaring a display function prototype only that displays a student test scores in the format (student name tab number of scores tab test scores )
is this right?
#ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; class StudentTestScores{ private:
[Code]...
and also how do we call the display function if it is in a class from the header file onto the main cpp file.
Ok so I am working on a game and I'm in the process of developing my Player class. Anyways, what I have is a keyboard function that is called in my main function to make a shape move.
void myKeyboardFunction(unsigned char key, int x, int y) { switch ( key ) {
[Code].....
But when I try to call it, trying to copy my previous method,
glutKeyboardFunc(Player1.playerControls);
I get an error
error C3867: 'Player::playerControls': function call missing argument list; use '&Player::playerControls' to create a pointer to member
I get an error saying it can't convert parameters. I would just like to understand why the arguments become a problem when I make the function a member of my class, when the first method I used is so easy.
I'm making a program that's essentially a Text-Based Fire Emblem game; it runs calculations and rolls dice and has all sorts of Goodies. However, I have hit a block to the tune of
#ifndef ITEM_H #define ITEM_H class Item {
[Code]....
Up Until I called up a Sword object, it worked fine. But when I compiled it, I got an Undefined Reference to Item::Item() error in Line 8 of Weapon.cpp.
Everything seems to be in order and I know my code still has mistakes. I'm just trying to get it to compile and it won't allow it. I've narrowed it down to when I call the functions in main but beyond that I have no clue.
#include <iostream> #include <cstring> using namespace std; void getSize(int num); void getSpace(int num, int ptr); void inputData(); void printData(); void destroy(); const int BIG_NUMBER = 100;
//pa4.cpp wirtten by Syd Frederick #include<iostream> #include<string> #include<fstream>
[Code].....
When compiling I'm getting a strange error that says :
/tmp/ccdt0Bf9.o: In function `main': pa4.cpp:(.text+0x1c): undefined reference to `synopsis()' pa4.cpp:(.text+0x1e7): undefined reference to `execute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' collect2: ld returned 1 exit status