C++ :: Normal Rand Generator - Class Functions

Sep 19, 2014

So I have an object of class NRG (Normal Rand Generator) which takes, as an argument to it's constructor, an object of class RG (Rand generator).

template<typename RG>
class NRG {
RG rg;
public:
NRG(RG r):rg(r){}
double operator()();

An object of this class will return a normal random when it's member function operator()() is called:

template<typename RG>
double class NRG::operator()() {
static int flag = 0;
static double N2 = 0.0;
if(flag==0)

[Code] ....

However, when I run this I get an error which says:

C:UsersavadhootDesktopb.cpp|69|error: 'template<class RG> class NRG' used without template parameters|
C:UsersavadhootDesktopb.cpp|69|error: expected identifier before 'operator'|
C:UsersavadhootDesktopb.cpp|69|error: two or more data types in declaration of 'operator()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

View 3 Replies


ADVERTISEMENT

C :: Difference Between Functions As Pointer And Normal Function

Aug 30, 2014

whats the difference between functions as pointer and normal function, eg:

void function1(void)
void *function1(void)

What is the difference between the two?I'm doing parallel programming and we use pointer functions (void *function1(void)) when calling threads. I want to why it is done.

View 6 Replies View Related

C++ :: Class With Random Generator

Feb 3, 2013

I have a class the generates a random number, do I put the radom generator function in the .h or .ccp file?

View 1 Replies View Related

C/C++ :: How To Access Linked List Functions From Stack Class Without Functions

Mar 20, 2014

I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.

We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.

As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.

NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.

Stack.h

#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;

[Code]...

View 3 Replies View Related

C/C++ :: Array Of Functions Pointing To In Class Functions With Arduino

May 3, 2013

At the moment im trying out with pointing to an array of functions. I got this working as following:

typedef void (* functionPtr) ();  
functionPtr functions[2][2]={{do11,do12}, {do21,do22}};    
void do11(){DEBUG_PRINTLN("11");}
void do12(){DEBUG_PRINTLN("12");}
void do21(){DEBUG_PRINTLN("21");}
void do22(){DEBUG_PRINTLN("22");}    
void loop(){
         A=0;
         B=1;
         functions[A][b]();
}  

But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:

error: argument of type 'void (Basic::)()' does not match 'void (*)()'

View 2 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

Jan 21, 2014

The compiler creates virtual table for the base class and also for the derived class whether we override it or not.

That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1

The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.

But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.

#include<iostream>
class A{
public:

[Code].....

View 1 Replies View Related

C++ :: Using Rand Function And Resetting Seed?

Nov 9, 2014

So I've actually used rand() happily without any problems so far, but now I faced a problem. I have an AI loop that initialized a random number between 1 and 2 at the beginning, so something like this:

while (condition) {
int random = rand()% 1 + 2;
if (random == 1) {

[Code] ....

So this doesn't really work since it doesn't actually assign a new random value. I came across srand(time(NULL)), which resets the seed, but I didn't quite get when to use it. Do I have to call it in every cycle of the loop? I'm not completely sure how this works.

View 2 Replies View Related

C++ :: Avoiding Rand To Generate Same Numbers

Mar 25, 2014

I tryed to create program I wich u can guess number, I made some if to prevent inputting 2 times the same number It works. I tryed to apply the same process for generating

#include <cstdlib>
#include <iostream>
#include <stdlib.h>

[Code]....

View 6 Replies View Related

C :: Simulate Roll Of A Dice - How To Use Rand Function

Sep 23, 2014

I saw a program in which it uses rand like this:-

d1=rand() % 6+ 1;

where, d1 is any integer. The program is to simulate the roll of a dice. The whole program is this:-

Code:

#include<stdio.h>
#include<stdlib.h>
main() {
int i;
int d1, d2;
int a[13];

[Code] .....

View 10 Replies View Related

C++ :: Using Rand Function In While Loop And Output Using Switch Statement

Jun 12, 2014

Basically I'm supposed to use a while loop to generate a random number and use a switch statement to output the appropriate information. I feel like I'm missing a few things that are very simple.

The errors are:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
warning C4700: uninitialized local variable 'randomNumber' used

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main(){
int i = 0;
unsigned int randomNumber;

[Code] .....

View 2 Replies View Related

C++ :: Card Rand And Suit - Adding Dynamic Arrays

May 1, 2013

I'm writing a program with two classes, one that contains a card's rank and suit and the other contains pile of these cards. I'm having trouble when it comes to adding a pile to another pile...here's what I have so far:

class Card //Card class declaration
{
private:
int rank; //invoking rank
char suit; //invoking suit
public:
void setCard(int r,char s); //determines card suit/rank

[Code] ....

I understand that I'm calling my pile arrays incorrectly, but I'm not sure why.

View 1 Replies View Related

C++ :: Use A New Class On Functions?

Feb 14, 2015

how use a new class on functions?

class test
{
public:
int a=0;

[Code]....

i did something like these with my image class, but i get wrong results.

View 6 Replies View Related

C++ :: Calling Functions Of Class And Outside With Same Name?

Apr 6, 2013

Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,

#include <iostream.h>
#include <conio.h>
void main();
void loop()

[Code]....

so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?

View 7 Replies View Related

C++ :: Communication Between Functions Of Class

May 19, 2013

I have a class as follows:

class testa {
public:
testa_b();
~testa_b();
testa_c();
}

Then I have defined some variables in testa_b:

testa::testa_b()
{
double aa = 1.0;
}

What is the easiest way to make testa_c() can use the aa defined in testa_b() (without declaring them in the class)?

View 3 Replies View Related

C++ :: Where To Put Non Class Member Functions

Sep 27, 2013

If you are doing some big program, usually, how do you organize the files? Put the class and its member in head file, but where to declare non member functions and where to define them? I don't want to put them all in one cpp file. If not, how to make them visible to the main cpp file?

View 4 Replies View Related

C/C++ :: How To Use Fstream Functions In A Class

Apr 4, 2014

I want to design a class that will open a text file and manipulate the data in it. And I have to use all these functions from fstream like ifstream, ofstream, seekg etc. The problem is that I can't get the first part to work (getting my constructor to open the file using ifstream). I've posted my test.h file below.

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#include <iostream>
#include <fstream>
#include <string>  
using namespace std;
using std::ifstream;  
class Test {

[Code] ....

View 2 Replies View Related

C++ :: Share Values Between Functions In Same Class?

Mar 22, 2013

Code:

#ifndef CANDIES_H
#define CANDIES_H
#include <iostream>
#include <fstream>
#include <Global.h>
class Candies

[Code]...

Code:

#include "Candies.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <sstream>

[Code]...

When I tested it, the file name in getName() outputted the right file name. However the file name in loadName() does not print out anything in the console window.

I want it to be so that the strings in [I]name_addTXT.c_str() in both functions are equal.

View 4 Replies View Related

C++ :: Get Access To Functions Of A Derived Class

Jul 11, 2013

I have a big problem with searching a solution for getting access on getters and setters of the derived classes of an interface.

Interface:

class IfParam
{
public:
IfParam();
};

Now I have a derived class for each needed datatype. E.g.

class Int32Param: public IfParam
{
public:
Int32Param();
Int32Param(IfParam* l_param);
int getValue();
void setValue(int l_value);
private:

[Code]...

My Problem now ist getting access to the getters/setters over the interface. I wish I could initialize the Params like this:

IfParam* param = new Int32Param();
param.setValue(12);

IfParam* param = new StringParam();
param.setValue("String");

But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how? I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.

View 7 Replies View Related

C++ :: Accessing Class Functions In A Vector?

Jul 24, 2013

How I can use class functions in a vector. I have 2 classes:

Gamemode class
bullets class

Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.

If the user presses the shoot button a new bullets class will be added to the Bullet vector:

b = new bullets;
Bullet.push_back(b)

Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.

I already set up a for loop which counts from 0 to the end of the vector:

for( int i=0;i<Bullet.size;i++)
{
}

My idea was to do something like:

if(Bullet[i].collision()) then erase Bullet[i]

But that doesn't work...

View 2 Replies View Related

C/C++ :: Calling Functions Inside A Class?

Nov 4, 2014

Im currently working on a class assignment and the pseudo code containing the instructions that I need to complete list:

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call Init() and pass 0, 0, 0, 0, and 0 as the parameters.
//--------------------------------------------------------------------------------------------------------------

My code for this is:

Point::Point(){
void Init(0, 0, 0, 0, 0);
}

The code I wrote for the function Init is here:

Point::Init(int mX, int mY, color mColor){
mX = 0;
mY = 0;
mColor = pInitColor;
}

My problem here is that whenever I try calling this function in the point class, I get an error next to void Init saying incomplete type is not allowed. Also visual studio is telling me that it expects a ')' after my first zero in that line.

View 1 Replies View Related

C/C++ :: How To Add Functions To Class In A Separate File

Jan 31, 2014

I could do the assignment if all i had to do was create a function within one file, call it in the main class, and that would be done with it. no, my teacher wants us to have our main program, a header and a separate class file and create functions in it in which we can then use in the main file.

This is the main file:

#include <iostream>
#include "call.h"
using namespace std;
int main() {
int length; //holds the length of the call in minutes
int hour; //holds the hour od the day(0-23 military time)

[Code] .....

View 7 Replies View Related

C++ :: Public And Private Functions With No Class?

Jul 7, 2012

I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that?

View 7 Replies View Related

C++ :: Optional / Default Parameters In Class Functions?

Aug 19, 2014

In the thread "Making a argument optional to function", it is stated that to set default values for arguments of a function you can simply do so in the function definition, like:

int myfunc(int a, int b, int c=3) {...}

This then automatically puts c to 3 in the function body if a call like myfunc(1,2); is made, if I understood correctly. However, this does not seem to hold for class functions. For example, something like:

class classy {
public:
int class_func(int, int, int); // class function prototype
}
int classy::class_func(int a, int b, int c=3) {...}

fails to compile. What I would like is to be able to call class_func outside of this class (by including it as a header in another macro), optionally specifying c. If c is not specified in the call, it should use a default value.

View 2 Replies View Related

C++ :: Passing Char Arrays Between Functions Of Same Class

Nov 16, 2013

I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.

Here's my code:

Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph

[Code] ....

View 4 Replies View Related

C++ :: Defining Member Functions Outside Class Definition

Jan 3, 2014

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class Calc {

[Code] ....

when i built it, it showed the following errors:

1>------ Build started: Project: rough, Configuration: Debug Win32 ------
1> rough.cpp
1>e:c programs
ough
ough
ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier
1>e:c programs

[Code] ....

Need sorting out the errors!!!

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved