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


ADVERTISEMENT

C++ :: Shared Library Throws Exception But Not Caught

Jan 10, 2013

A test program of mine loads a shared library (.so file). A function call in the shared library throws an exception that I am trying to catch in the main function of my test program. (I know that exception is being thrown for sure, I wrote the library to do that.)

However in the main program, the exception is not being caught. The flow of program goes past the catch block like no error has occurred. I am using g++ and I load the shared file using -l option. Only trying to load the program statically I got the following error:

/usr/bin/ld: cannot find -lmy-shared-library
collect2: ld returned 1 exit status

Why the exception is not getting caught.

View 9 Replies View Related

C++ :: Writing Custom Allocator - Throws Exception

Mar 4, 2012

Does the requirements have changed for writing custom allocators with C++ 0x11?

A custom allocator (that uses a fixed array as its memory pool) that I have been using successfully for some time, now throws an exception!

It runs fine under VS2008 but bombs out with VS2010.

View 14 Replies View Related

C/C++ :: How To Handle Exception In Constructor

Apr 14, 2012

If a class A contains an array of objects of another class B.. And in the constructor of A, while constructing objects of B, any one object throws an exception, then how can we guarantee to release all the memory acquired.. so that there is no memory leak..

class B{};
class A{
public:
  B Array[100];
  ...
};  

In the above code, if in constructor of A, suppose 99 objects of B are constructed successfully, but 100th object throws exception, then how can we guarantee to release all the memory acquired by the other 99 objects?

View 1 Replies View Related

C++ :: How To Design A Smart Pointer Class

Nov 11, 2014

smart pointer class is the one that take in charge of release allocated resource when itself destroyed.

recently,i want design a smart pointer class, it take in charge of release resource allocated by new or new[].

my problem how to design destructor, you should determine the pointer ptr is pointer an array or a single object.

the structure of this smart point class may be:

template<class T>;
class smart_pointer{
public:
smart_pointer(T* p):ptr(p){};
~smart_pointer(){

[Code] ......

View 6 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++ :: 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++ :: 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++ :: 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++ :: Singleton Class - Auto Seems To Return Wrong Type

Jul 18, 2013

I am trying out a technique for a singleton class:

// access controlled singleton, accessed through function "instance()"
// singleton is constructed in this function
// so that constructor and destructor will be used
class single {
// private constructor/destructor

[Code] .....

Playing around with the code in main(), I am having trouble with auto:

single& s = single::instance(); // works fine
auto a = single::instance(); // error ~single() is private

When I make the destructor public, the output of the program is:

ctor
dtor
dtor

So I fixed this by typing auto&. I'm still confused though, why wouldn't auto know I am returning a reference?

View 2 Replies View Related

C/C++ :: Catching Base Class Exception As Reference

Jul 23, 2013

class BaseExcep {
protected:
      std::string message;  
public:
      BaseExcep(std::string);
      virtual void printException();

[Code] .....

Here i need to find which exception type is actually thrown.

Whether it is DerivedExcep1 or DerivedExcep2 or  BaseExcep itself.

I can't use dynamic_cast in this, since it is a reference ....

View 5 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related

C++ :: Invoking Base Class Constructor From Derived Class?

May 15, 2013

I understand it is done like this

// Calling the base class constructor
explicit CCandyBox(double lv, double wv, double hv, const char* str="Candy"): CBox(lv, wv, hv)
{
...
}

But how does the compiler know that you are initializing the base "part" of the object?

Or is this the entire reason initialization lists exist, to separate this confusion?

View 4 Replies View Related

C++ :: Using Constructor Within Constructor In Same Class

Feb 28, 2012

I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:

error: type "mainClass" is not a direct base of "glavna"

This is the program I tried:

Code:
class mainClass {
private:
int x,y;

Code] ......

View 6 Replies View Related

C++ :: Creating One Class Object In Constructor Of Another Class

Sep 20, 2013

Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.

View 6 Replies View Related

C :: Guess 4 Int Array With How Many Are In Right And Wrong Positions

Feb 11, 2013

For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.

So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc....

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time (NULL));

[Code] ....

View 3 Replies View Related

C/C++ :: Array Switches Wrong Values

Jun 7, 2014

I have a game that requires the switching of tiles. When I try to switch the blank tile (_) with 5, the 5 switches with the 8 instead of the tile switching with the blank tile. The code behaves the same with top row. I have observed that this only happen in the row and column bound by [0][0]; For example the four top left tiles ([8, 7, 5, are 4]) are behave erratically.

Here is sample code!

//swap if columns are equal and blank is to bottom
else if ((blankCol==tileCol) && (blankRow-tileRow==1)) {
temp = board[blankRow][blankCol];
board[blankRow][blankCol] = board[tileRow][tileCol];
board[tileRow][tileCol] = temp;
return true;
}

I am thinking perhaps there may be a double switch but then again why does it for only the left column and top row.

Attached image(s)

View 2 Replies View Related

C/C++ :: Direct X Tool Kit Throws Errors When Loaded In

Oct 21, 2014

I have a project which I started. then tried to add the directx toolkit to the project referenced it but now my program wont load.

View 1 Replies View Related

C++ :: Find Index Of String Array - Get Wrong Return Value?

Apr 17, 2014

Here is my code to find the index of a string array whose string is equal to the query string. I have checked the program can return the correct index, but the cout result is totally wrong.

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

[Code].....

View 4 Replies View Related

C++ :: Wrong Return Type When Returning Char Array As Pointer

Apr 25, 2014

I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.

Here is a snippet:

Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}

Visual studio is saying that the return type doesn't match.

View 3 Replies View Related

C++ :: Five Different Tables - Array Sort Exception Return

Dec 18, 2014

I am creating 5 different tables each one has 20000 more elements more than the previous when i try to sort them with the sort algorithm quick sort for the first table tha has 20000 elements runs grate then it throws this exception how is this fixable ?

Exception : terminate called after throwing an instanceof 'std::bad_alloc' this application has Requested the Runtime to terminate it in an unusual way. Please contact the applications's support team for more information. Process returned 255(0xFF) . here is the code that gives this return :

Code:
#include<time.h>
#include<stdlib.h>
#include <string.h>
#include <ctime>
using namespace std;
int * Create_Table(int table[],int N);

[Code] .....

View 10 Replies View Related

C++ :: Class With Enum And Constructor

May 29, 2013

I really confused with constructor (default constructor and constructor with parameters)

I coded this problem

and I worked almost, but I stock in constructor

Code:
class Tier {
public:
enum TIER_MAKE

[Code] ....

This is tier class and I have to finish constructor in class car (for simple, I skip detail code) -red things are the parts from class Tier

Code: Car()
: make(NULL), passengers(0), fuelcap(0.0), efficiency(0.0), tier(Tier::nexen)
{ }

[Code] ....

And someone said default constructor part has to be this

Code:
car( Tier::TIER_MAKE p_tiermaker = Tier::nexen )

//after i skip
but default constructor should be no parameter...? isn't it?

View 1 Replies View Related

C++ :: Class Course / Constructor Errors

Mar 22, 2014

# include <iostream>
# include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
class Course
// Creating the class Course

[Code] ....

Errors: Warning1warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

[Code] .....

I have to create an Array of type Course and then fill its member dats using various member functions. Those errors are caused by some Constructor defect, which I dont really know what it is.

View 2 Replies View Related

C++ ::  Can Use Class Constructor To Run Code In CPP By Itself?

Jan 13, 2015

I recently designed a struct like this

// MyMap.h
typedef std::map<std::string, std::function<void ()>> MyMap;
extern MyMap g_mymap;
// MyMap.cpp
My Map g_mymap;

[Code] ....

It looks useful to implement strategy pattern because it makes a fully separate code block. So I can add a function to the map simply by compiling a source file. It's very simple. I don't need to edit another file.

But when I use it for my existing project, It makes some linking and runtime errors.(vs 2012). I can't recognize exactly why because it is a huge project. Anyway, I have a question that - Is this a safe use of class constructor?

I know that there is no fixed order of running, but in this case I think it doesn't matter. because they are independent. But it is not a common pattern, so I can't decide to use it.

View 3 Replies View Related

C/C++ :: Add Copy Constructor To Class?

Nov 15, 2012

This is my class there is a problem with my copy constructor .. is that correct ??

struct Node {
    Type info;
    Node<Type> *next;
};  
template <class Type>
class Queue

[Code] ....

View 1 Replies View Related

C++ :: How To Redefine A Constructor From Parent Class

Jul 14, 2014

Firstly I don't really know if this is possible.

This is my Class Diagram: [URL]...

github: [URL]...

I want to redefine the price object of the Book Class. However price is defined at Products Class.

I want the price value change according to the marker value, which is a Book attribute.

If the marker is blue, price gets a value of 10 (e.g.), if it has another value, price is equal to 20.

View 10 Replies View Related







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