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


ADVERTISEMENT

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++ :: 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++ :: Error - Invalid Initialization Of Non-const Reference Of Type

Feb 11, 2013

I am trying to use the Singleton design patterno on Linux, but it is not working:

in the .h I have:

Code:
public:
static ErrorH& Instance();
private:
ErrorH() {};

In the .cpp I have:

Code:
ErrorH& ErrorH::Instance() {
static ErrorH& self = ErrorH();
return self;
}

The errors I get are:

Code:
g++ --g -c ErrorH.cpp -o ErrorH.o
ErrorH.cpp: In static member function "static ErrorH& ErrorH::Instance()":
ErrorH.cpp:9: error: invalid initialization of non-const reference of type "ErrorH&" from a temporary of type "ErrorH"
make: *** [ErrorH.o] Error 1

This code works on Windows, how can I get it to work on Linux?

View 2 Replies View Related

C++ :: Passing Struct Arrays To A Function By Value Not By Reference?

Mar 16, 2013

I am working on incorporating a function in to an already existing piece of code, I have incorporated the function fine as far as I am aware.

The problem I have is that I am trying to pass two int arrays to the function, so that i can manipulate and compare them "the values will be changed the originals cannot be changed"

I am having trouble pulling the information out of the already created array, I am able to pass the pointer reference for the single value which is not exactly what i want "best_prog".

My function is below I have commented the memcpy parts and also the majority of the code isn't there cause it is not needed to see make the copy work.

int edit_distance(int index) {
struct prog *progp = &population[best_prog];
/* The struct of best prog not sure if i need one for the other prog I am trying to compare it with the one below doesn't work as intended.*/
//struct prog *progp = &population[];
int editdistance = 0, ar1 = 0, ar2 = 0, a = 0, b = 0, j = 0, x = 0;

[code].....

View 12 Replies View Related

C# :: Reference Type When Passing Struct To Parameter?

Jan 24, 2015

Does putting a ref when passing a struct to a parameter work? Say i have this code

struct struct1 {
public int i;
public void show() {
Console.WriteLine(i);

[Code] ....

The output doesn't change. The result is

0
100
500
0

View 1 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

View 9 Replies View Related

C++ :: Creating A Struct Within Struct Node?

Feb 28, 2015

Im having trouble creating a struct within a struct node. the program suppose to hold students firstname, lastname, and gpa in a node therefore creating my linked list. Line 26 keeps saying that cannot convert parameter 2 from 'studentType to std::string

#include <iostream>
#include <string>
using namespace std;
struct studentType{
string firstname;
string lastname;
double gpa;

[code].....

View 2 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++ :: 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++ :: 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++ :: 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 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++ :: 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

C++ :: Class Member Variable Initialization?

Dec 18, 2013

Is it possible to initialize class member variables in their definition statement instead of using a constructor?

Is the code bellow correct?

class rectangle
{
float a=0;
float b=0;
public:
string color="Red";
...
};

Which C++ Standard allows it?

View 2 Replies View Related

C++ :: Two Dimensional Array Declaration And Initialization

Mar 28, 2013

I'm having trouble declaring and initializing a two-dimensional array using the C++11 standard conventions. I would like to know how to do it in C++11 style as know how to use the old style.

the exception im getting is:

c++11_array_exp.cpp:37:3: error: too many initializers for ‘std::array<std::array<std::basic_string<char>, 6ul>, 22ul>’

you can find my code here:

[URL]

View 3 Replies View Related

C/C++ :: Explicit Initialization Of Function Templates

Feb 28, 2013

Why will i use explicit instantiation of a template, for a type? If I do not use explicit instantiation, the template is used to create the necessary function then what is the use of explicit instantiation?

template <class Any>
void Swap (Any &, Any &);
// template prototype  
template <> void Swap<job>(job &, job &);
// explicit specialization for job

[Code] ....

In the above eg. explicit instantiation is done for char type. What is the use of this?? If the compiler can make use of the template to make a fn for char type.

<eg. taken from C++ Primer>

View 4 Replies View Related







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