C++ :: Using Variable In Pow Function?
Oct 15, 2014I'm trying to figure out what is wrong with this:
static_cast<int>((pow(x, counter+1)) % 10)
it says "more than one instance of overloaded function "pow" matches the argument list"
I'm trying to figure out what is wrong with this:
static_cast<int>((pow(x, counter+1)) % 10)
it says "more than one instance of overloaded function "pow" matches the argument list"
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;
Im starting with C. Like I said in the title, how do I assign the value from a function to a variable? I mean I have this function:
Code:
int EnteroAleatorio(){
rand();
return rand();
}
and I would like to assign the value of EnteroAleatorio to a variable in my main function, but when I try to do it and compile, I got the next error: non-lvalue in assignment
I need to set a function to a variable of some kind. Then later in the program it needs to run the function that is set to the variable. The variable doesn't need to change after it is set to a function, it just needs to be able to be set to a function. So maybe I don't need a variable? What do I do? :3 Is this even possible? :o
Example:
if (PosRampYes == 0)
{
SomeVariableOrSomething = FirstFunction();
}
else
{
SomeVariableOrSomething = SecondFunction();
}
//later:
SomeVariableOrSomething; //so if PosRampYes is set to 0 then this line would run FirstFunction()
I am trying to call a function through a variable
The error
|error: no match for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'void')|
|warning: statement has no effect [-Wunused-value]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
#include <iostream>
#include <fstream>
[Code].....
#include<iostream>
#include<stdlib.h>
using namespace std;
int Name(),Minimum(),Maximum();
int main() {
int name=Name(),minimum= Minimum(),maximum= Maximum();
[Code] ...
There are error to let user to key in minimum and maximum values, i would like to know whats the problem?
Expected output: 20
But what I got is: 22
Why. While calling sub function it should take the global variable am I right
insert Code:
#include <iostream>
using namespace std;
int a=0;
void sub()
[Code] ....
I get an error when i try to compile this code. I tried to allocate memory in main function and that works. But why it doesn't work in function? I think that there is something wrong with function argument, but not sure.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct Word
[Code].....
I'm currently writing a poker game and am trying my best to avoid using global variables. I have a few variables in int main() which i was hoping to use to store the value of each players hand. I then created a function which calculates the value of the hand but cannot get this value back into the main function.
For example:
Code:
#include <iostream>
using namespace std;
void getValue(int value) {
value = 4;
[Code] ....
Is there any way i can get the value of value using this function? If not what can I do?
This code is for a program that allows you to play a guessing game or arithmetic game and shows your total score from both as you go along. The program works fine the only problem I'm having is with the score. Is there a way to call the score value from the outside function into main?
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//prototype functions
void menu();
[Code] .....
So trying to pass the value of score from the outer function int guessGame() into the score print statement in choice 3 of the main function? Oh and the "17 -turn_count" was just part of the requirements of the assignment.
I am trying to make a function that allows me to allocate memory to a "mem" variable and setting each of its chunk's status to FREE. FREE is defined as 0. Below is my code of the function.
Code:
int allocate(mem *mm, int num_chunks, int chunk_size) {
int i;
mem *temp;
if((mm = (mem *) malloc((num_chunks + 1) * chunk_size)) == NULL){
perror("Failed to Malloc
[code]...
mem; If my function works the way it should, it should print out five 0 because that is how I set them in the function, but this is not the case. I've looked at my function for 2 hours, but I could not figure out any logical error. Now, I think my problem lies with my limited knowledge of pointer arithmetic. On the other hand, when I insert 1000 as the second argument into my function, it gives seg faults, which is not the case for smaller values like 5, 10, 15, etc.
I was wondering if one could write a function that could accept one or the other variable type.
Ex: I have 2 arrays, int** and double**, and a function
Code: void PGMWrite(double** Matrix, int Matrix_dimension){.....}
Is there any way to change the function to
Code: void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
And then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?
I need to keep a static variable in a member function of a class that I have many objects of. I've had some trouble with it, and when I read up I found that such variables are static across all instances. Is there any way around this?
View 3 Replies View RelatedI want to get a random number from this function but idk if I did this function right....
int random(int x) {
srand(static_cast<unsigned int>(time(0)));
int randomN = rand();
x = (randomN % 10) + 1;
return x;
}
So, one can do stuff like this using #defines:
#include <iostream>
#include <array>
#define x arr[0]
#define y arr[1]
#define z arr[2]
class Point {
[code]....
... that is, to be able to reference the same data by "member variables" as by referencing a stl container. But defines are the devil's work - adding in a "#define x arr[0]" is a dangerous statement. I'd really like some nice clean C++ method (C++11 or C++14 are just fine) to do this without defines, but so far I'm drawing a blank. If arr wasn't an STL container, if we just wanted a pointer-based array, I could do it this way:
class Point
{
...
float x __attribute__ ((aligned (sizeof(float))));
float y __attribute__ ((aligned (sizeof(float))));
float z __attribute__ ((aligned (sizeof(float))));
float*const arr = &x;
};
... but you obviously can't do that if arr is an STL container.
The best I've come up with is to make x, y, and z function pointers, but then you can't call them like p.x, you have to call them like *p.x(), it's not very clean and I'd expect some added overhead. One could go even uglier and make x, y, and z be instances of some custom class with overridden operators that reference arr[], but that seems like it'd be just getting ridiculous in terms of overhead (both coding and performance)
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???
I try to create small project in order to better understand how key word static works with templates . However some compiles errors crush my plan.
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> main.cpp
1>c:all myс++ha level 6solution level 6solution level 64.2b - ex1. static variable for array def size. templatesarray.cpp(40): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
[Code] .....
what should be the prototype for the following function.
Code:void addition(int x, ...);
I am getting compilation errors. I have written the prototype as :
Code: void addition(int, va_list);
I need a variable that will just hold a function that I can change in the middle of the application, even to different function type with different amount of parameters ... is this even possible? At the moment I have this
struct REPLY {
string *sReply;
function<int()> special = [](){return 0;};
REPLY()
[Code] .....
And then change it back to blank function returning 0 after calling it
(reply.special)();
reply.special = [](){return 0;};
This works fine (there is no reason why it should not, right? Now, if I want to have another function, let's say
void test2(string str) {
MessageBox(NULL, str.c_str(), NULL, NULL);
}
How do I point this one to the variable special, when I want to call it like that (or something similar)
(reply.special)("test string");
is this even possible? if so, how? i tried to create function pointer (didnt compile at all) or use template (neither did this) and how to do this as I discovered functional lib just a while ago.
I need to create a function that takes as an input a variable number of scalars and returns the biggest one. Just like std::max() does for 2 elements, but I need it for an undefined number of elements, can be 2, or 5, or 10 etc.. How to approach this?
What I need it for: I'm working with a bunch of vectors, maps, etc. and I need to find out which has the most elements. So I was thinking that I should end up with something like
int biggest = max(vector1.size(), vector2.size(), map1.size(), ...);
In the following code:
#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}
Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?
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] ....
Say you had:
class Foo{
public:
//...
void funky();
[Code] .....
Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?
If I define the variable 'total', everything works well without any problem. But if I define it inside the function 'sum', I get irrelevant results, because each time the function gets executed, the variable total gets defined again, losing its value already assigned to it.
Do I have to use a global variable? Is there any way that I can do with without using a global variable?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int total;
int sum(int a, int B)/>{
if (a < B)/>{
//printf("The total is %d, a is %d, and b is %d", total, a ,B)/>;
total += a;
[Code] ....
In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.
I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????
for example
class A {
public:
int add(int x) {
return x+x;
[Code] .....
I am having following pointer variable declaration
Code: Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};
Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.