C++ :: Exceptions Thrown During Initialization?

Jul 8, 2014

How exactly would one go about throwing an exception from an object's constructor?

Take a look at this snippet:

#include <iostream>
#include <exception>
class ExceptionFoo : public std::exception {
virtual const char* what() const throw() {

[Code].....

View 8 Replies


ADVERTISEMENT

C# :: Writing To CSV File Thrown Off By Quotes

Apr 24, 2014

When one of the cells in my DGV contains quotes, it throws off the data in the CSV file by splitting the string into seperate columns.

foreach (DataGridViewRow row in dgv_NewData.Rows) {
if (!row.IsNewRow) {
for (int i = 0; i < row.Cells.Count; i++) {
sb.Append(row.Cells[i].Value + ",");
var value = row.Cells[i].Value.ToString();

[Code] ....

View 2 Replies View Related

C/C++ :: Error Thrown By Compiler / Identifier Expected And Declaration Terminated Incorrectly

Jan 30, 2015

Error message is identifier expected and declaration terminated incorrectly.

//to define a class Employee
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class cEmp {

[code]....

View 7 Replies View Related

C++ :: Developing Application Without Exceptions

Feb 27, 2015

Is it possible to develop any application without exceptions? I don't want to try and catch exceptions and remove it.but I want to write codes which should never create exceptions is it possible?

View 3 Replies View Related

C++ :: Custom Exceptions With Default Behaviour

May 24, 2013

I've been pondering which of these 2 approaches would make for the best interface for a library: Defining custom exceptions with specific names for different error scenarios but with standard behaviour, or simply using the predefined exceptions from the STL.

This is my current approach:
Code:
namespace rpp
{
class ConnectionError : public std::exception
{
public:
ConnectionError(const std::string &p_err);

[Code] .....

This seems to make for more descriptive code but it adds no functionality and the implementations are completely identical, which seems "off" to me, somehow.

View 8 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C++ :: Catching Exceptions From Initializer List

Jan 4, 2015

In order to test catching exceptions from an initializer list, I deliberately did bad practice by hard coding an argument to a ctor that would cause a std::bad_allocto be thrown. Obviously better practice is to send a variable, but that would cause a compile error, so I hard coded a value.

The program I wrote creates Prime Numbers up to a specified limit which is an argument to the ctor of type std::size_t. The program works fine IMO, using g++ in cygwin:

$ time ./PrimesExe
Limit is 2000000
148933 Primes Created

real 0m1.210s
user 0m1.123s
sys 0m0.046s

Now when I send something invalid like a negative number or something too big for std::size_t, the program seems to run indefinitely, when compiled with g++ under cygwin. I haven't tested it yet on Linux.

However, if I do the same on VS2013 express, it takes about 15 seconds to print the expected caught exception message. I was not expecting it to take so ridiculously long compared to the reasonable amount of work involved in doing primes up to 2 million.

I have read up about what is involved in catching exceptions: stack unwinding, keeping track of what needs to be destroyed etc. But this is 1 object with 1 ctor argument, no Base classes or any other complications. So why such a long or indefinite amount of time?

This whole example is probably contrived, and I am wondering whether exceptions is the right tool for this - it is similar to the divide by zero problem, or could be considered a programming error to call a ctor with a bad argument?

Also, catching an exception thrown by an initalizer list seems a bit awkward in that one seems to have enclose the creation of the object and all subsequent uses of it (and any code in between ) in the same try block, otherwise it goes out scope. I suppose I could try to write a wrapper function that returns a smart pointer reference to a valid object, but I would have to test the validity of it's return too. That's the awkward part - there is probably a better way?

Are there any recommended ways of recovering from initializer list exception, that is, to allow the user to enter a new hopefully valid value and try to create the object again?

View 2 Replies View Related

C++ :: How To Handle The Exceptions Not Caught By Try / Catch

Nov 2, 2014

Here is an example,

Code:

int main()
{
try{
int y = 0;
int x = 2/y;
}
catch(...)
{
cout<<"catch it"<<endl;
}
return 0;
}

If you try, you will find out that catch(...) couldn't catch exception divided by zero. How'd we catch such exceptions?

View 4 Replies View Related

Visual C++ :: Getting Exceptions On Windows 7 And 8 On Different Parts Of Code?

Feb 25, 2015

I'm Getting exceptions on Windows 7 and Windows 8 on different parts of the Code? Why this can be happening, why the difference in behavior?

View 6 Replies View Related

C :: Initialization Of A Pointer

Apr 27, 2013

Here is a function,which deletes the spaces of a string...

char *removespaces(char *s1) {

Code: char *s2=s1;
int i,j=0;
for (i = 0; i<strlen(s1); i++){
if (s1[i]!=' ') {
s2[j]=s1[i];

[Code] .....

why I have to initialize the pointer *s2 with the first element of the array s1...???If I don't initialize the pointer,or initialize it with something else,I get a segmentation fault...

View 10 Replies View Related

C :: Initialization Of Function Pointer

Apr 28, 2013

I would like to initialize an arry containing function pointers with adresses of functions that have a variable number of arguments.

Below the code that works in principle. I would however get rid of the warning message during compilation pointing to the initialzation of the funtion pointers in the array. How do I need to cast the pointers to the functions ?

Code:
gcc func_ptr_init.c
func_ptr_init.c:28: warning: initialization from incompatible pointer type
func_ptr_init.c:32: warning: initialization from incompatible pointer type

Code:
#include<stdio.h>
unsigned char func1_ptr(unsigned int* if_desc, int* result_code) {
*if_desc = 1;
*result_code = 1;
return(0);

[Code] ....

View 8 Replies View Related

C++ :: Array Of Struct Initialization

Apr 4, 2014

In a book I'm reading, the author has the following struct:

struct VertexPos {
XMFLOAT3 pos;
};

Where XMFLOAT3 is a structure with x,y,z floating point values within. He declares and initializes an array as the following.

VertexPos vertices[] =
{
XMFLOAT3(0.5f, 0.5f, 0.5f),
XMFLOAT3(3.3f, 5.6f, 3.6f),
XMFLOAT3(-4.5f, 2.2f, 6.4f)
};

It doesn't make sense to me why this is working. The way my mind is thinking about this is that vertices is an array of type VertexPos, so then why can it be initialized with objects of type XMFLOAT3?

View 2 Replies View Related

C++ :: In Class Static Map Initialization

Nov 17, 2014

I have a class containing a map member that I want to initialize at declaration time. I know I can do it in the cpp file but I'm having a problem with the order of initialization (static initialization order fiasco).

My questions are:

Is it possible that the scenario in which, the Test's constructor's implementation and the map initialization instruction are in the same cpp file and constructor is called when the map is not initialized yet, could happen?

Is it possible to initialize the map in class like I did? I get these errors:

in-class initialization of static data member 'std::map<std::basic_string<char>, Test*> Test::a' of incomplete type
temporary of non-literal type 'std::map<std::basic_string<char>, Test*>' in a constant expression

If yes, does this initialization resolve the static initialization order fiasco?

class Test {
public:
static std::map<std::string, Test*> a = {};//this is an error
Test(std::string ID) {

[Code] ....

View 4 Replies View Related

C++ :: Initialization Of Pointer Arrays

Jan 1, 2014

The below example is an initialization of a pointer array:

char *month_name(int n) {
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}

Here we have an array of character pointers. The pointers are stored in name[i]. And they point to the characters of the i-th string, which are stored somewhere else in memory. But Is the character pointer pointing to the first character in the character string that is stored somewhere else in memory? I assume so because a string itself is an array of characters. And if that is the case, how does the pointer know what the last character should be? Does it just search for the null terminator?

View 2 Replies View Related

C++ :: Struct Reference Initialization

Nov 16, 2014

I have a custom struct hierarchy that goes vaguely like this:

struct Base
{};
struct Derived1 : public Base {
int num;

[Code] .....

I'm using "Base" simply as an umbrella struct, so I can access any of the derived structs with a "base" reference(&).

The issue I'm having is, I have a class that has a data member that is a reference to the struct "Base" but, I'm getting an error that says my constructor for this class doesn't provide an initialiser for that data member.

I've tried intialising a derived object for the reference, like so:

MyClass:MyClass() {
Derived1 d1;
d1.num = 0;
mBaseRef = d1;
}

But, it doesn't work...

View 1 Replies View Related

C++ :: Initialization Of Frames Variable

Jan 19, 2015

I tried to initialize the frames variable but when I go to debug it, it just gives me 30 errors compared to the one error when I don't initialize the frames variable.

#include "Gfx.h" // general gfx lib I made for SDL
#include "Input.h"
#include "General.h"
#include "Sprite.h"
#define screenw 620
#define screenh 480

/** CHARS ARE DONE */
//int mousex; int mousey; int mouseon = 0;
//int red = 0; int green = 0; int blue = 0;

[Code] ....

View 4 Replies View Related

C++ :: Construct Maps Initialization

May 5, 2013

I've a problem with the map construct. I wrote this class in which there are maps:

class Features {
private:
map<const string,GenericFeatureContainer*> featuremap;
map<const string,GenericFeatureContainer*>::iterator it;
int size;
public:
}

And I have the following constructor:

Features::Features() {
map<NULL,NULL> featuremap;
it=NULL;
size=0;
}

Is that correct? otherwise what is the correct syntax?

View 2 Replies View Related

C++ :: Segmentation Fault And Also Bad Initialization

Feb 25, 2012

Code:

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define FILEPATH "./input.txt"
#define FILEPATH2 "./copy.txt"

[Code] ....

The problem is probably the fact that I'm using the variable s in the wrong way, but as I'm very bad at C and C++, at least so far anyway, I've no clue what's wrong. Is it my size that I passed in the marked method?

Also, how do I tell it, later, once this starts to work, to pass the file name of the file it'll copy to as the first param and the file it reads from to the pipe as the second param?

I'm supposed to be reading from a file, and as I was given some code, but it's probably in C and not C++, even if it is, I'm still not that great at C++ either, but anyway, I'm to have the program read from the file and write to the pipe and have the child read from the pipe and write to the output file.

FileCopy copy.txt input.txt

View 14 Replies View Related

C++ :: Char Array Initialization And Display

May 16, 2013

The following is something I am not clear about. Multi dimensional char arrays and the displaying of them.

Code:
#include <iostream>
using namespace std;
main() {
//char test[5][5]

[Code] .....

The commented out expression didn't run at all but the double quotation mark one did, unfortunately, it gives me a hexadecimal display. How can I get it to display like this:

*****
*****
*****
*****
*****

View 5 Replies View Related

C++ :: Why The String Provided For Initialization Is Too Long

May 9, 2014

I'm not understanding why the string provided for initialization is too long?

Code:

int main()
{
char array[1] = "A";
return 0;
}

View 6 Replies View Related

C++ :: Static Constant Member Initialization

Jun 5, 2013

I am having a problem concerning a static const member variable I want to use to set a certain property of my class during development time. The question actually concerns proper implementation as I do have a solution that "works" at least. The variable should denote the size of a member array which I don't want to allocate on the heap due to serious performance issues. So here is my code:

//MyClass.h
class MyClass{
public:
static const int MyArraySize = 256;
private:
int MyArray[MyArraySize];
};

This works but it's not nice for two reasons:

1) It doesn't separate interface from implementation. I would prefer to define the variable in the corresponding .cpp file but it doesn't work:

//MyClass.h
class MyClass{
public:
static const int MyArraySize;

[Code] .....

If I delete the line int MyArray[MyArraySize]; the above code works but when I use it to define the size of the array I get a "constant expression expected" error for the line int MyArray[MyArraySize]; which makes sense as the compiler does not know the value of MyArraySize when he reaches int MyArray[MyArraySize]; and therefore can not allocate the memory. Of course I can move MyArray to the heap like that:

//MyClass.h
class MyClass{
public:
static const int MyArraySize;
static const int MyValue;

[Code] .....

But as I mentioned before this causes a remarkable loss of performance.

Something like the following does not work:

//MyClass.h
class MyClass{
public:
static const int MyArraySize = (int) pow(2, 8);
private:
int MyArray[MyArraySize];
};

This gives a "constant expression expected" error for the line static const int MyArraySize = (int) pow(2, 8);

Interestingly the following code works:

//MyClass.h
class MyClass{
public:
static const int MyValue;
};

//MyClass.cpp
#include "MyClass.h"
const int MyClass::MyValue = (int) pow(2, 8);

So if I use pow outside of the class definition I get no errors. Is there any solution to those problems? So what I want is:
1) Don't allocate the array on the heap
2) Separate interface from implementation
3) Being able to use functions like pow to define MyArraySize
4) Not use global variables

View 19 Replies View Related

C++ :: Using Array Initialization List For Own Class

Jun 17, 2014

i'm currently working on a research project and i've been given some specifications

Is there a way i can access/use the array initialisation list i.e

{value,value,value}; .

For my own class? Like this

myclass foo={value,value,value};

View 3 Replies View Related

C++ :: Invalid Initialization Of Reference Of Type

Oct 12, 2013

The error is : invalid initialization of reference of type 'ArrayT<float>&' from expression of type 'const Arrat<float>'...The above errors occur when I tried to do the following code , with operator* overloading :

const ArrayT<float>& b1 = A*A;
ArrayT<float>& c2 = b1*A;// <---- this line makes the error
//b1 is a const ref of ArrayT<float>
//A is just a normal object of ArrayT<float> created by ArrayT<float> A(blah,blah,blah);

The following are the list of operator* overloading :

template <class T>
ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) {blah,blah,blah}
template <class T>
const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) const

[code]....

I want to use for error multiplication above, but not success.

View 15 Replies View Related

C++ :: Initializing Map - No Matching Constructor For Initialization

Nov 18, 2014

I am trying to create a `std::map` from `std:: string` to a pointer to member function type. I'm initializing the `map` with an initializer list (braced). When I compile, I get a message from the compiler: No matching constructor for initialization.

Example: [URL] .....

View 4 Replies View Related

C++ :: Initialization - Set All Entries In Array To 0 Or A Particular Number

May 19, 2013

How do you set all the entries in an array to 0 or a particular number...

View 3 Replies View Related

C++ :: Constant Data Member Initialization

Apr 9, 2014

Here's a part of my program. What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.

class Book {

friend void CompPrice(Book &,Book&);
//friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private:
//private data members

[Code]...

View 1 Replies View Related







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