C++ :: Declaring New Instance Of A Class

Nov 6, 2014

I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.

Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){

[Code] ....

View 9 Replies


ADVERTISEMENT

C++ :: Class Function That Uses Instance Of Its Child Class As Argument

Mar 1, 2013

I am facing a real-life problem, it can be simplified as below:

#include <iostream>
using namespace std;
class B;
class A {
public:
void f1(A a) {}
void f2(B b) {}

[Code]...

There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?

The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.

View 11 Replies View Related

C++ ::  Declaring Object Of A Class In Another Class

Jul 14, 2014

Information:
I'm using Code::Blocks v12.11.
(I'm using C++/SDL2, but I think that's of no relevance)

Problem:
I create a class named "CSprite" in a "Sprite.hpp"-file. I create a "Sprite.cpp"-file, which includes the "Sprite.hpp"-file. I define the methods of the class "CSprite" in the "Sprite.cpp"-file.

When I try to create an object of "CSprite" in the class named "CPlayer" in the file "Player.hpp" I get an error message. (<-- Looks complicated I know, the code example will be more usefull than this)

Error in the build messages:
C:UsersLinoDocuments1 Data LinoFreizeit1 ProgrammierenC++ & SDL2The Running ManCPlayer.h|30|error: 'CSprite' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|

Code Example:

Sprite.hpp
#ifndef _SPRITE_HPP_
#define _SPRITE_HPP_
class CSprite {

[Code] .....

What did I miss? Did I include the wrong file? Or did I Forget to include the file? Why do I get the error message?

I also tried it with a pointer declaration and the "->" Operator but I got the same error message. I know I could just write a new function to load the texture in my "CPlayer"-class but this would not really answer my question.

View 15 Replies View Related

C++ :: Instance Of Class A Vs Class B Derived From Class A

Jul 3, 2014

There are two ways to access the members of class A inside class B:

1) Making an instance of class A in class B
2) Deriving class B from class A

So what is the basic difference in both ways as we can do same kind of work with both ways?

View 1 Replies View Related

C++ :: Why Would A Class Allocate An Instance Of Itself

Oct 4, 2014

I'm currently learning the Qt framework and doing my first tutorial. Straight away I saw something that baffled me:

notepad.h

Code: namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow

[Code] ....

Note the ui pointer and the heap allocation in the class constructor; I can't wrap my head around why one would do this. What's going on here?

View 5 Replies View Related

C++ :: Vector Is Different For Every Class Instance

Feb 12, 2014

Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!

//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__
#include <iostream>

[Code].....

View 1 Replies View Related

C++ ::  How To Have A Class Return Instance Of Itself

Nov 24, 2012

I was wondering if (in C++) you can instantiate a class (class foo) then have said class return the already instantiated object. (foo::instance())

In other words, can I have a class return it's-self via it's own methods? I want to be able to create a class (i.e. class foo) early in my program so it is already setup and ready to go. Then, farther down the line, I want to be able to call functions from that class without having to pass that object as an argument to my calling function. Can I do something like so:

MyClass::ReturnSelf()->foo();
or
MyClass::ReturnSelf().foo();

View 13 Replies View Related

C/C++ :: Creating New Instance Of A Class?

Mar 2, 2014

I have a background in c# and am very frustrated with c++. If I created a class in c# like so:

public class Memory{
int x = 0;
int y = 0;
int height = 0;
int width = 0;
string firstname = "Bob";
string lastame = "Chester";
}

and then created a new instance of this class from a separate class by doing:

public class Main{
Memory mem = new Memory();
}

I have raked the internet for a way to create a new instance of a class in c++ while keeping its default values and have come up empty handed.

View 13 Replies View Related

C++ :: Declaring Array Inside A Class

Apr 18, 2013

class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------

int numLights;
int lights[MAX_LIGHTS];

[Code] .....

I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.

How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.

View 6 Replies View Related

C++ ::  Passing Class Instance To Function

Jan 17, 2014

I know that it is possible to pass a class instance to a function, but in my experience, if said function changes any variables of the class, they don't actually get changed. For example, we have class object, that has a member int number = 5. Lets say we have two functions, func1() and func2, which are not members of class object. If we pass object to func1() which, lets say, increases number by 5 (so now number = 10), at the end of that function number still = 5.

Is there a way to bypass this and have functions alter class variables permanently?

I know that I can pass variables by reference, but, in my experience, such a thing does not work with vectors (which I am also dealing with), so simple passing the desired variables by reference won't work.

View 5 Replies View Related

C++ :: Cannot Create Struct That Contains Instance Of A Class

Sep 4, 2013

I'm trying to learn as much C++ as I can. I was writing a program that mixes linked lists and classes. There is the class "Obj" which only holds an integer called 'data' and the classic "struct node" structure for linked list, but this time the "node" structure will hold an instance of "Obj" Class and the next* pointer.

#include <iostream>
using namespace std;
class Obj {
private:
int data;
public:

[code]....

View 2 Replies View Related

C++ :: Finding A Specific Instance Of A Class

May 22, 2013

So I have a class that is like this:

class card {
public:
int id;
int val;
};
card card1;
card1.id = 1;
card1.val = 2;
card card2;
card2.id = 2;
card2.val = 45;

etc...

So my question is firstly, is there a better way to implement this? (a vector of classes or something maybe?) and how can I call up a specific instance of the class. For example, if I want the val of a specific instance of the class, how best can I do that?

View 3 Replies View Related

C++ :: Change Value Of Instance Variables From Another Class?

May 6, 2014

I'm trying to change the values of some instance variables in my Controller Class so that when the user inserts values into main class it changes for Controller.

class Controller {
public:
Controller();
~Controller();
double PCFreq;
__int64 CounterStart;

[Code] ....

The user should be able to choose which foo the want to use. So I create an object of controller in main like this

Controller* con = new Controller()

Now my issues is, when I take user input (an integer) and try to do this

con->choice1 = choice1;

only the object of con's choice1 is = to user input.

However back at the class for Controller, choice1 hasn't received a value.

I can't initialize through Controllers constructor because I get the user input through a switch statement and the range of con would only be as far as the case.

View 2 Replies View Related

C/C++ :: Allow User To Create A New Class Instance?

Dec 4, 2014

I am creating a program that allows a user to create multiple 'sequences' and multiple 'filters' and then apply a filter to a sequence.

Each sequence and filter is an array of values.

How do I go about allowing the user to create a 'new' sequence and then store the location so I can access it again later? Once they have created a new sequence they can go on to create another sequence and ten maybe a filter and then another sequence etc etc .. and then they can select sequence 1 and edit the values if they so wish.

They would be asked how many sample values for the sequence, and then I would create a sequence with that many values and an id (1,2,3,4...). They could then enter this id to view/edit the sequence.

The entering/editing values part I am fine with. I just don't know how to allow them to create multiple new instances of a class without using an array so something like..

sequenceClassName somearray[10];
int i;
*create a new array*
somearray[i].create_class(how_many_samples)
i++ //so next sequence they create is 2,3,4.. etc

- this then calls the member function that creates an array using 'sample_values = new float[how_many_samples]' and the user can input their data and edit it whenever by entering the id which will correspond to the somearray[i].

However that approach only allows them to enter a maximum of 11 sequences. It all depends on how big I make that initial array and it just seems like the wrong way to do it.

( how to interact with them, just how to create multiple classes and recall them later to access the data!)

View 2 Replies View Related

C/C++ :: Declaring Map Global Or Wrap In Singleton Class

Jul 13, 2014

#include <map>
#include <iostream>
#include <stdexcept>

[Code]....

SuperSmartPointer<int> ptr4((int*)ch); this line gives error error as double deletion will occur.

Solution :
1) make the reference map global variable.
2) wrap the map in a non-template singleton class.

View 1 Replies View Related

C/C++ :: Declaring Constant Struct As A Member Of Class

Oct 2, 2014

I would like to have a unmodifiable standard of WAVEFORMATEX defined as a member of a class of mine. Something like:

class InputTest {
public:
const WAVEFORMATEX StandardWaveFormat;
public:
void TakeInput(WAVEFORMATEX pFormat);
};

Then in my cpp file to hard-code the values:

WAVEFORMATEX InputTest::StandardWaveFormat {
//Instantiate WaveFormat -- PCM standards
StandardWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
StandardWaveFormat.cbSize = 0; //extra information sent over stream. Usually ignored in PCM format.

[Code] ....

I get the following errors starting with the header file:

Error1error C2146: syntax error : missing ';' before identifier 'StandardWaveFormat'
Error2error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

both associated with the "const WAVEFORMATEX StandardWaveFormat; " line.

Here's a link to the WAVEFORMATEX struct: [URL] .....

Then the cpp source code is probably way off. Let me know if you'd like to see the errors associated with that.

View 11 Replies View Related

C++ :: Create 5 Instance Object Of Class Test?

Apr 27, 2014

I am new to C++. I am trying to create 5 instance object of class Test. I wonder if these two codes are the same.

Test testArray[5];
//which one is the correct way or what is the correct way?
for(i=0;i<5;i++)
{
Test testArray;
}

View 2 Replies View Related

C++ ::  How To Create Global Object / Instance Of A Class

Aug 25, 2013

I'm new in object oriented programming. I need creating a global object/instance of a class, that can be used by any function.

View 19 Replies View Related

C# :: Creating Instance Of A Class Given Its Assembly Object

Jan 15, 2015

Let's say I reference a dll in my project that has an abstract class in it:

public abstract class Module {
public string type;
public abstract string doSomething();
}

And then I have an Assembly object, loaded from a dll that is not referenced in project, but rather loaded during run-time, containing:

public class Tester : Module
{
public static string type = "Test";
public override void doSomething() {
//Stuff being done
return "hello";
}
}

How can I get the value of "type" in the class that is loaded during runtime? How do I use the "doSomething" method of it and get the returned object?

View 8 Replies View Related

C# :: Using Different Instance Of Class Depending On User Input?

Jan 13, 2015

Here's the problem I want to intialise different classes based on user input. I've tried using an IF statement but the scope of an if statement would mean the rest main program wouldn't be able to see it.

View 12 Replies View Related

C++ :: Template Class Instance - Proper Datatype

Oct 20, 2012

I have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.

View 5 Replies View Related

Visual C++ :: Creating Instance Of MFC Derived Class

May 23, 2013

I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class). But it was generated with a protected ctor. Here's the code explanation with comments.

I'm thinking all the normal classes of the Doc/View template are created starting with this code, but within the template code base.

Code:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CViewSwitchView));

But I have generated "another view" using the "Add Class" Wizard, it's a derived class of mfc CFormView which I named ViewForm. However I'm having a problem creating an instance of it because of the generated protected ctor and pulls a compile error of not being able to access ctor. Below are the header and implementation files of this said ViewForm class. How to create an object of this view ? Did I go about it all the wrong way since it's not in the doc/template group ?

// ViewForm.h file
#pragma once
// ViewForm form view
class ViewForm : public CFormView {
DECLARE_DYNCREATE(ViewForm)

[Code] ....

View 5 Replies View Related

C++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

#include <vector>
#include <iostream>
using namespace std;

[Code].....

View 19 Replies View Related

C++ :: Global Class Instance In Multi-File Project

May 3, 2013

I'll just tell you in short how my Problem looks like: I should implement a New Class in a SourceCode i didnt write myself. The source code is extremely sized (i think approx >100.000 Lines of Code), so i dont want to change too much in it in order to get my Implementation done.

MY problem looks simplicified like that: Starting from 3 classes and my new class the pseudo-code looks like that:

Class1(){
float* m_CalibX, m_CalibY;
.. }
Class2(){
char* m_ImageData[];

[Code] .....

So, i need Parameters from 3 different classes to insert in my NewClass. The 3 Classes dont know anyting about each other. So, i need to implement a Class-Instance from Type NewClass which is known by the other 3 Classes. I did solve it in this way:

//ClassInstance.h

#include "NewClass.h"
static NewClass ClassInstance

I just wrote a headerfile with a class-instance which is getting included by the other 3 Classes. So they all know the same Instance and writing their Parameters into it. Is this a decent solution or could it happen to get bugs/ logical mistakes with it?

View 2 Replies View Related

C++ :: Polymorphism On Class Instance Is Not Directly Applicable To Array

Mar 19, 2013

Below code produces run-time segmentation fault (core dumped) when execution reached line 53: delete [] array

Code 1:

#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {
protected:
int ID;

[Code] ....

Output of code 1:

Constructor of A: Instance created with ID=1804289383
Constructor of A: Instance created with ID=846930886
Constructor of A: Instance created with ID=1681692777
Constructor of A: Instance created with ID=1714636915
Constructor of A: Instance created with ID=1957747793
A::showID() -- ID is 1804289383
A::showID() -- ID is 846930886
A::showID() -- ID is 1681692777
A::showID() -- ID is 1714636915
A::showID() -- ID is 1957747793
Try to delete [] array..
Segmentation fault (core dumped)

Question: Why does segmentation fault happen in code 1 above?

View 9 Replies View Related

C/C++ :: Simple Arkanoid Game - Deleting Instance Of A Class

Mar 24, 2015

I am making a simple arkanoid game. When the ball hits the block, i want the block to disappear. How would i go around this? i am working in visual studio. I got the collision and everything working.. Here is my code;

int main(int argc, char** argv){

//initialisation
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Graphics Window",NULL); //set name of project

//variable declaration
SDL_Surface*screen;
boolrunning;
intgameState;
float startTime;

[Code] ....

View 1 Replies View Related







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