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


ADVERTISEMENT

C++ :: Const Static Members In A Template Class?

Jan 17, 2013

I have a little problem with template classes and their specialization. Here is a short example:

template <typename T>
struct A{
// some typedefs

[Code]....

The above example is not compiling, because of the assignment of the const static double. Double needs a constructor, but that doesn't work (or seems not to work) with static.

I'm not sure, if it works at all in C++ that way. All I want is a template struct with some typedefs and a constant which is different for different specializations. Don't think it has to be static, but that would be better style, wouldn't it?

View 3 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++ :: Class Constructor With String Const Pointer As Parameter

Feb 19, 2013

I know my product.cpp constructor is wrong but how can I set my private variables.

//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H

[Code].....

View 2 Replies View Related

C/C++ :: Constructor To Initialize Each Members Of Array?

May 3, 2014

Need a C++ constructor to initialize each members of an array. how to give value for for each elements of an array declared as a class object according to the users input.

View 1 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++ :: Calling Const / Non-const Overrides

Oct 5, 2013

Are there other ways of calling a const/non-const override? I want to defined some functions in terms of others, particularly accessors which might or might not require constness- in order to not copy & paste code. This is my current solution:

struct dumbArray {
dumbArray(unsigned int size):
m_array(new int[size]){
}
~dumbArray(){
delete m_array;

[Code] .....

View 10 Replies View Related

C++ :: Const Static Integral Members

Jan 16, 2014

I've been having a problem concerning the initialization of const static integral members with floating point calculations. I'll let his sample program do the explaining:

class Foo {
public :
Foo() {}
const static int samplerate = 44100;
const static unsigned short tempo = 120;

[Code].....

I know you can't initialize const static non-integral types on the same line on which they're declared, but I don't see why even an implicit cast to an integral type should be disallowed. I make my calculations using doubles, so I'm surprised that even though it should degenerate into an integer - it's still a problem for the compiler.

View 1 Replies View Related

C++ :: Calling Constructor From Constructor Set Pointer To Null

Jan 25, 2014

VS 2012 / Windows 7 64 bit

class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}

[Code] ....

it works until the myclass(int* p) calls myclass()

then the MyPointer will become CCCCCCCC (NULL value)!

is there a way to stop the second constructor from resetting the pointer value to null?

View 3 Replies View Related

C++ :: Custom Array Class For A Project

Oct 15, 2014

I am currently working on a custom "Array" class for a project, and I have run into an error I don't quite understand the source of. The relevant code is as follows:

template<typename T> class Array {
private:
T errValCopy;
public:
T __errVal__;
uint16 __size__;
T* __ptr__;
Array(const T& errorValue);

[Code] ....

When I try to run the following code:

Array<Array<int>> a(Array<int>(-1));

The error log tells me there is no appropriate default constructor available. If I understand it correctly, "default constructor" refers to the constructor which lets you just write Array<int> a; instead of Array<int> a(...);, but I can't see where in the code such a situation occurs...

View 2 Replies View Related

C++ :: How To Put File Data Into Members Of A Class - Array Transferring

Mar 10, 2013

I'm trying to put file data into members of a class. Remember to type in the file name you want to open. Cool feature right? I just had Dbase.txt so I chose that.

Fixed stuff in the .txt. Now I need to figure out why it only does 1 set and then ends.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class INFO {

[Code] .....

Dbase.txt:
Bob
Guy
Programmer
M
9999.99
40
------------------
Little
Guy
Little Brother
M
0.0
3
------------------

View 14 Replies View Related

C++ :: Custom Dynamic Array Class And Allocator

May 10, 2014

I attempted to create a dynamic array class for use in my engine (due to problems regarding a dll-interface with the standard library), so I tried at making a standard-compatible allocator template class first. After I "finished" that, I went on to work on the dynamic array class itself.So I finish the dynamic array class, and test it with the standard allocator. It works perfectly, but when I test it with my custom allocator class, it fails terribly.

To make sure it wasn't my DynamicArray class that was causing issues, I tried using the custom allocator on the std::vector class template, and it didn't work either. IMy DynamicArray class code:

// Represents a dynamic array, similar to the standard library's "vector" class.
template<typename T, typename A>
class DynamicArray
{
public:
DynamicArray() :
data(nullptr),
elements(0),
capacity(0)

[code].....

The "Request" and "Free" functions are my engine's equivalent of malloc and free (or new and delete). I allocate a large buffer (16 mb), and through those functions I distribute the memory to where it's needed.

View 9 Replies View Related

C++ ::  Initializing Const Char Member Variable In Constructor?

Jan 23, 2015

I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?

I want to do something like this, but I know this isn't allowed:

/* class.h */
class foo {
public:
foo(*parentWindowPtr);

[Code] .....

I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.

View 4 Replies View Related

C++ :: How To Return Element In Array That Is Inside A Class

Nov 16, 2014

I have a program where I roll a die X number of times and need to print how many times it lands on each side. I tried to create an array in the class aDie that increments each time the corresponding number is rolled but when I go to call and print it in the main my out put is 0. I just picked how many times it landed on the side 4 just to see if it works and it doesn't.

#define ADIE_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
class aDie {

[Code] .....

View 3 Replies View Related

C++ :: Dynamic Char Array For Class Constructor

Nov 14, 2013

I'm writing a class that has two constructors. However, I can't get them to work quite right. One constructor has a parameter of an int (with a default value of 0) and the other has a parameter of a C-style string.

First of all, are these function prototypes correct for the constructors?

MyInt(int n = 0);// first constructor, int param, default value 0
MyInt(const char * c);// second constructor, c-style string param

Both constructors work fine in some cases but don't work in all cases. Here are some potential calls to these functions that are supposed to work:

// These two work fine
MyInt x(12345),
y("9876543210123456789"),
// The array assignment doesn't work when the value is negative
// I'm not allowing negative numbers, but I want to create the object and assign the array to 0
r1(-1000),

[Code] .....

Here's the private data from the class (from the header file):

private:
int currentSize,
maxSize;
char* digits;// Pointer to an array of digits

// Increase the size of digits array by 5
void Grow ();

View 2 Replies View Related

C++ :: Multiple Constructors Calling Parent Constructor

Dec 6, 2013

I have a class that extends another class, and I want multiple constructors in the child class, but the child constructor needs to call the parent constructor. This is what I have

In the child class:

ChildClass::ChildClass() {
ChildClass(1);
}
ChildClass::ChildClass(int i)
: ParentClass(i) {
// do stuff
}

In the parent class:

ParentClass::ParentClass(int i) {
// do stuff
}

In my main program:

ChildClass child1;
// do stuff with child1
// breaks
ChildClass child2(1);
// do stuff with child2
// works fine

Using the default constructor breaks my program at runtime, but using the one with a parameter works fine. The default constructor calls the other with the same thing as the main part in the program, so I would think this should make no difference, but obviously that isn't the case.

View 3 Replies View Related

C++ :: Derived Class Constructor Using Base Class Constructor?

Jan 1, 2013

Is this example correct? This example from a book

Constructor of the Base Class
Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}

Constructor of the Derived Class (inherited from the base class)

Student(char* n, int s=0, char* i=""):
Person(n, s)

Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?

View 5 Replies View Related

C++ :: Using Static Method To Check Arguments Before Calling Constructor?

Mar 1, 2013

I would like to avoid throwing things in constructors as much as possible.

Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.

View 14 Replies View Related

C++ :: Calling Constructor Inside Another One (No Matching Function To Call)

Nov 7, 2013

I've written an Array class to create 1d,2d and 3d array and it works fine for every test : example of the constructor of the array class for 2d case:

Array::Array( int xSize, int ySize ) {
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}

It works fine , but when i need to use this constructor inside of other constructor, i get the "no matching function error" ,
part of my code:

class StaggeredGrid {
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}

[Code] .....

View 2 Replies View Related

C++ :: Smart Array Class - Constructor Throws Wrong Exception

Jan 24, 2014

Writing a smart array class for my C++ class. I'm running into a problem where the constructor apparently throws the wrong exception. Compiled on G++ with C++11 extensions enabled.

Code:
// headers
#include <iostream>
#include <utility>
#include <cctype>
// stuff we need from namespace std
using std::cout;
using std::cin;

[Code] .....

When I try to check the error-handling code, when I enter a size less then two, Array's ctor throws InvalidSize, and gets caught, all good. But when I enter a letter, the ctor also seems to throw InvalidSize!

View 14 Replies View Related

C++ :: Friendship From Derived Class Method To Base Class Members

Jul 15, 2014

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();

[Code] ......

View 3 Replies View Related

C++ :: Can One Constructor Of A Class Call Another Constructor Of The Same Class

Mar 19, 2015

to initialize this object? Why C++ FAQ says no? Here is my code,

Code:
class A
{
public:
A(int x, char c);
A(int x);

[code] ....

I don't have any trouble to call the constructor A(int x, char c) from another constructor A(int x).

View 10 Replies View Related

C++ :: Set And Get The Value Of Different Members In A Class

Jun 28, 2013

I am new to c++ programming and i have written a simple class program to display the name and duration of the project

#include<iostream>
class project {
public:
std::string name;
int duration;
};

[Code] ....

Now i am trying to the write the same program with the usage of member functions using set and get member functions. i have tried the following

#include<iostream.h>
class project {
std::string name;
int duration;
// Member functions declaration

[Code] ....

I am not sure whether above code logic is correct, how to proceed with it.

View 3 Replies View Related

C++ :: Static Const Class Instances?

Jun 17, 2012

i have this rather large class, which (in a way) somehow resembles a custom dialog control). This control is supposed to display data, which it does just fine. To do so, it maintains a

byte settings[10];

array, which holds information on how to display the data.

There are multiple ways to represent this custom set of data.In order to remain flexible in representing it, i thought of implementing some sort of DisplayProvider, which can be registered to the base class and provides that settings byte array.

Preferably, i would now have a set of static const instances of this provider.Using a struct would work nicely here:

PHP Code:

struct DisplayProvider
{
int settings[10];
}
static const DisplayProvider prov1 = {1,2,3,4,5,6,7,8,9,0}; 

The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.I would end up with something like this:

PHP Code:

class DispalyProvider
{
baseclass* owner;
 int settings[10];
void PreProcessing(...);//ends up calling the owner.Processing(...) function
}; 

The main thing here is, that i dont really see a way to create a stock of default "static const DisplayProvder = {...}"s, as i could when using a struct.

View 7 Replies View Related

C++ :: How To Define A Class Which Have Two Members

Sep 16, 2013

I want to define a class, which will have two members, for example, vaporPressureStatus and vaporPressure

enum vpStatus_t {nonesense, unknown, known, saturated};
class pore_t {
public:
vpStatus_t vpStatus;
double vaporPressure;
};

when vpStatus is nonsense and unknown, the vaporPressure should not have a value; and if I calculate out a value for vaporPressure, the vpStatus can be set as known.

I am wondering if there is any set, pair or other structure can hold this two members together, so that when I change one's value, the other guy will also change accordingly.

View 3 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







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