C# :: Calling A Class Within Itself?

Apr 12, 2015

I created a simple program to understand it

class TestClass {
private int x = 10;
TestClass a = new TestClass();

[Code].....

I know this is recursion but how do the compiler do this? How can it call itself when it hasnt even completed initializing every object it has? Why do VS allow this?

View 1 Replies


ADVERTISEMENT

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++ :: Calling Base Class Constructors From Derived Class

Mar 30, 2013

I'm having some difficulties in understanding the topic which I stated above.

View 5 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++ :: Calling Object Within A Class?

May 10, 2014

class abc {
public:
int i;
abc * foooo;
};

How do you call * foooo? Say I create:

abc a;
* foooo would have some values for int i.

To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?

View 5 Replies View Related

C++ :: Calling A Function On Derived Class

Dec 29, 2012

I'm trying to call a function on a derived class that's in a vector of it's base class. I've made the code really simple for illustration purposes:

class Sprite {
virtual void update();
}
class Enemy : public Sprite {
virtual void update();

[Code] ....

I want to be able to just call update() on the items in the vector and the derived class update() functions be called. Currently, it always calls the Sprite update, which makes sense, but it's not what I want. Is there a way to call the derived update function without knowing the type of the derived class?

View 6 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++ :: Calling Function In Custom Class Using Pointer

Nov 6, 2013

Right now I have code

#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;

[Code] .....

it gives error ...

View 1 Replies View Related

C++ :: Segmentation Fault When Calling Factory Class Method

Mar 23, 2014

I'm still working on my process API, as in my previous posts. Right now I'm trying get my class portable so I can use it for any language/compiler by using a factory design pattern. I'm having problems figuring out how to call the methods properly from my interface pointer in my factory class without causing a segmentation fault.

Code: main.cpp
Code: #include "exports.h"
#include <iostream>
using namespace std;

[Code]......

View 5 Replies View Related

C++ :: Program To Calculate Fine Using Class - Calling Constructor

Sep 7, 2013

// Program to calculate fine using class.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class library {
long bookid;
char bookname[25];

[Code] ....

In the above program, how do I call the diff. constructors in main?

View 5 Replies View Related

C++ :: State Design Pattern - Calling A Class Member

Oct 17, 2013

In the following code example of the State Design Pattern, in the main code at the bottom it defines an instance of class Machine, and then calls Machine::off;. Why doesn't it instead call fsm.off;?

Machine fsm;

Machine::off;

Then I tried imitating that by adding a class Abba, and then doing:

Abba a;
Abba::DoStuff();

but that didn't work. Why?

Full code example:

// StatePattern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class Machine {
class State *current;

[Code] ....

View 3 Replies View Related

C++ :: Calling Method Of Specialized Template Base Class In Subclass

Feb 10, 2013

class IFoo {
virtual void Bar() = 0;
};

class FooAbstract {
virtual void Bar() {}

[Code] .....

How to call the Bar() method from FooTemplate in FooDerived::Bar()?

View 5 Replies View Related

C++ :: Calling Custom Constructor Of Element In Array Whose Class Has Const Members?

Apr 15, 2013

If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?

I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.

Code:
class Foo {
public:
Foo();

Foo(int x, int y);

[Code] .....

View 4 Replies View Related

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

Aug 19, 2014

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

class MainObject;
class SecondaryObject;
class SecondaryObject {
public:

[Code]....

View 10 Replies View Related

C++ :: Reducing Code Duplication From Common Code Calling Common Class

Apr 13, 2014

I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.

However, Is designing a complete class just for a few functions over kill?

Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?

View 4 Replies View Related

C++ :: Calling Subroutines Within DLL

Feb 20, 2013

I am using a dll written in FORTRAN to do some calculations and here is how I have set up the program. I have created following files:

C++ source files:
main.cpp
File1.cpp
Header Files:
Datamain.h
DataFile1.h

[Code]....

Here are my questions:

1) I actually want to call Sub2dll from the main program. But the above program does not build and it gives me the following error: In function 'int main()': Sub2dll was not declared in this scope.

When I build this program with the call to Sub2dll in main commented out and instead put that call in File1::Setup function things build well and runs giving the answer expected. How can I make the call to Sub2dll from main.

2) I am including "Datamain.h" in main. However when I build it with out the declaration int Datamain::Index; in main, it gives me the following error: undefined reference to Datamain::Index When I add that declaration to main the error disappears.

I am usiing Code::blocks with MinGW compiler. I have not put the argument list for the subroutine calls since there were not errors in that part.

View 3 Replies View Related

C++ :: Calling Function In DLL

Jun 8, 2014

I have this sample code, that calls a function in a DLL. The function Callback is provided to the DLL as an argument, in order for the DLL to notify my program of relevant changes.

sample:

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winbase.h>
#include "TcAdsDef.h"
#include "TcAdsApi.h"
 using namespace std;
 void _stdcall Callback(AmsAddr*, AdsNotificationHeader*, unsigned long);
 
[Code] ....

I would like to change this code, so that there is a Main class that opens the connection and there are several separate classes (as below) that register themselves for a specific variable and get notifications if that value is changed. The reason for this is that I want to get several notifications for several independent events and I don't want them to mix. I figured this should look something like this:

class.h
#ifndef INACLASS_H
#define INACLASS_H
 #include "Main.h"
 class InAClass {
    public:
        InAClass(Main* mainClass, std::string iolocation);

[Code] ....

Unfortunately this gives me an error:
error: cannot convert 'InAClass::Callback' from type 'void (InAClass::)(AmsAddr*, AdsNotificationHeader*, long unsigned int)' to type 'PAdsNotificationFuncEx {aka void (__attribute__((__stdcall__)) *)(AmsAddr*, AdsNotificationHeader*, long unsigned int)}'

At first I thought this was because I don't have the namespace "using namespace std;" on top, but then I should be able to find something that specifically needs to come from the std namespace and is not marked as such. I don't want to rule the option out, but so far I could not find anything like that.

An alternative explanation might be that the Callback function needs to be global, but if I make it global, how can I distinguish between several Callback functions?

View 7 Replies View Related

C++ ::  Calling All Instances Of Object?

May 28, 2013

So I am wondering how I would go about calling a function for all instances of an object. I tried googling it, but all I saw was solutions like making an array of pointers to the objects. What if I don't know how many objects there will be? Isn't there an easier way?

View 19 Replies View Related

C++ :: Calling Template Functions

Feb 21, 2013

How do I call these functions from Mechanical.h???

Mechanical.h
#ifndef MECHANICAL_H_
#define MECHANICAL_H_
class statics { public:

template<class T> struct Inertia_types {
T Rec(T _x, T _y);
T Tri(T _x, T _y);

[Code] ...

I am trying to create templated functions which I can apply all data types to except for strings and other types such as wchar. Am I also writing these correctly? This is my first attempt ever doing this.

View 1 Replies View Related

C++ :: Calling Char From Function

Aug 3, 2013

Every time I try to use the function SaveNewCD, it doesn't write to file correctly. It writes the ~, three characters, then goes into an infinite loop.

#include<iostream>
#include<fstream>
using namespace std;
int SaveNewCD();
int OpenCD();
int main() {
char ArtistName[25];

[Code] .....

View 5 Replies View Related

C++ :: Calling A Function Through A Variable?

Jul 2, 2014

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].....

View 3 Replies View Related

C++ :: Ofstream In Calling Function

Apr 3, 2013

I am trying to use ofstream to write in a txt file in a function called recurrently. for a simplified example:

void func_write(double x) {
ofstream myfile;
myfile << "the result = " << x << endl;
} int main() {
ofstream myfile;

[Code] .....

To this stage, it does not work, because the myfile in func_write cannot write in the txt file opened in main function. I don't want to open, append and close the txt file each time the function is called, that will take more time to execute all (imagine with 500 calls).

View 2 Replies View Related

C++ :: Calling Functions From Templates?

Mar 14, 2013

Having issues calling my arguments from my templates, when i declare x and n in the main it comes up with errors

code below:

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
int intInput1, intInput2;

[code].....

View 4 Replies View Related

C++ :: Calling Array From One Function To Another?

Dec 28, 2013

I want to call the array from one function to another all function not a main function

View 2 Replies View Related

C# :: Calling Same Method Twice With Different Output

Dec 24, 2014

Allow users to enter their name and favorite saying in a single method that gets invoked two times. If I can only return one value at a time, how am I suppose to get name and favorite saying out of UserInput()?

static void Main(string[] args) {
string displayName, favoriteSaying;
DisplayInstructions();
Console.WriteLine();//readability

[Code] ....

View 3 Replies View Related

C# :: Calling Event From Another Form

Mar 11, 2015

I want to update a label from Form2. When I click on the button on Form 2, the textbox on Form2 should update the label on Form1 and take me to a certain website. What ever URL is typed into the textbox on Form2 is what website URL the label on Form1 will take me to. The label on Form1 can say something such as You can check out my website here!!! and when they click on the saying it takes me to their website.

View 2 Replies View Related







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