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


ADVERTISEMENT

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++ :: Non Constant Member Function Being Called Inside Constant Member Function?

Dec 28, 2012

#include <iostream>
class Hello {
public:
void Test() {

[Code].....

As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .

View 5 Replies View Related

C++ ::  How To Get Relative Address Of Member Of Class Of Struct

Apr 2, 2014

How to get relative memory address of members of Class or Structure ? I want to auto scan the members of Class/Struct, and show the address/value like the "watch window" in debug mode of popular C/C++ IDE software.

View 2 Replies View Related

Visual C++ :: Declaring Member Variables In Windows Form App

Sep 30, 2014

I have created a windows form project in visual studio so that I can use a windows form to interact with the game class that I'm creating, but I'm running into some problems.

For one thing, I would like to be able to call Image::FromFile() one time only during initialization, and store the result in a member variable (called mBGImage). From what I can tell, the variable needs to be of type String^ (this caret symbol is new to me, but I understand it is the "managed code" version of the standard pointer, which would look like String*).

When I try to compile the following code (located in my header file) I get the error :

"error C3265: cannot declare a managed 'mBGImage' in an unmanaged 'BSG::BSGame'".

How can I store the result of Image::FromFile() permanently in my class?

When I try to declare a global variable of type "Image^", I get "error C3145: global or static variable may not have managed type System:rawing::Image ^"

#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;

[Code] ......

View 2 Replies View Related

C++ :: Constant After Member Function

Aug 27, 2013

#include<iostream>
using namespace std;
class Student{
public:
int age;
int rollNo,marks;
string name;
void AddEntry();

[Code] .....

error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|

why and how can I solve it?

View 7 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++ :: 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++ :: Static Constant Member Variable

Jun 6, 2013

What is the problem with the following code is? It compiles with Visual C++ 2012 but does not with g++:

//a.h

#ifndef Loaded
#define Loaded
using namespace std;
class MyClass{
public:
static const int MyStaticValue = 200;

[Code] ....

If I try to compile this using the command

g++ a.cpp b.cpp

I get an "undefined reference to 'MyClass::MyStaticValue'" error for the line "A = MyClass::MyStaticValue;" in main(). The strange thing is that if I change the line to "A = (int) MyClass::MyStaticValue;" it works fine and the output is

200
200

as expected.

The code also compiles under g++ if I move the defintion of MyStaticValue from a.h to a.cpp by const int MyClass::MyStaticValue = 200;

View 5 Replies View Related

C++ :: Defining A Constant Data Member?

Dec 21, 2012

Programe #1
// file.h
class File {
public:
static const int var = 9;
};

[Code]....

Program#1 is running fine, but program#2 gives linker error:

error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj

I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?

View 3 Replies View Related

C# :: Why Can't A Constant Field Be Of Non-built-in Struct Type

Dec 9, 2011

Code:

class T
{
enum E { }
struct S { }
interface I { }
delegate void D();
class C { }
}

[code]....

All of the above are possible except the constant field of a type that is a struct. I can see why the non-string reference types would default to the only literal expression they can represent - null - and if I'm not mistaken the default value of a struct is an instance of the struct with all its fields set to their default value, but why is it that a const struct field is not possible? is there a reason why or is it just the way c# was written? oh and by the way, what's the idea of allowing enum types to have a default constructor?

View 4 Replies View Related

C++ :: Struct Inheriting From A Class Or A Class Inherit From A Struct?

Mar 9, 2012

I just read and have known for a while that classes are private (members and inheritance) by default and structs are public. But my question then comes what if.. a struct inheriting from a class or a class inheriting from a struct?

View 3 Replies View Related

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 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++ :: 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 :: Initialize Data Member In Struct

Mar 28, 2013

I was looking at some linked list material and was wondering something. Can you initialize a data member inside a struct like in C++? i.e.

Code:
typedef struct node
{
int data;
struct node * next = NULL; // this is the line in question
} LLnode;

View 3 Replies View Related

C :: Cannot Assign Struct Member Values

Feb 13, 2015

I'm a C beginner trying to assign struct member values to other struct members to create a list.

I've tried dot notation, pointer notation, strcpy, memcpy, memmove and normal assignment.

Nothing has worked.

cust_list.h

View 5 Replies View Related

C++ ::  Static Member In Struct Array?

Jun 25, 2013

Here's the definition of my struct:

struct Speaker {
static int numElem;
string name;
int number; // Phone
string topic;
float fee;
};

// IN main() FUNCTION
Speaker s[10];

The goal is for numElem to keep track of how many of the 10 elements are in use. However, I'm not sure the proper way to access the element, if it's even possible.

View 7 Replies View Related

C/C++ :: Segfault When Accessing Member Of A Struct

Mar 18, 2014

The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,

[code]
loadDB(db, configFile);
printf("%d",db->relationCount);
fflush(stdout);

View 5 Replies View Related

C/C++ :: Assigning Value From Struct Member To Outside Structure

Jun 20, 2013

I have the code bellow, and i print the data but cannot assign it to global variable.

struct frequency_data {
 char frequency[10];
 int32 frequenca_mesatare;
 int16 PWM_F;
 int32 PR2;

[Code] ....

View 5 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 :: Set Struct Member Variable For Structure Inside Def

Mar 12, 2014

This is with Linux gcc

Code:
typedef struct _a
{
int id;
} a;
typedef struct _b
{
a my_a;
my_a.id = 1; // error: expected specifier-qualifier-list before "my_a"
} b;

I get error: expected specifier-qualifier-list before "my_a"

I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?

View 10 Replies View Related

C++ ::  assigning To A Member From Struct Obtained By Overloaded Operator

Jan 27, 2014

I'm trying to assign a value to a member of a struct that I called via an overloaded [] operator. I have the following code for the struct:

typedef struct {
float r, g, b, a;
float operator [](int pos) {
switch (pos) {

[Code] ....

And what I wish to do is

MyStruct a;
a[0] = 0.5;

Is it possible with a struct? How to express this to search engines so I haven't been able to find anything about it. If this is not possible with a struct, is there a way to define something that can do all the following things:

SomeStruct test = {0.5, 0.5, 0.5, 1};
test.g = 1.0;
test[0] = 0.0; // test[0] would be equivalent to calling test.r
float somevalue = test[3]; // test[3] would be equivalent to calling test.a

I hope I've been sufficiently clear.

View 2 Replies View Related

C++ :: System That Communicates With Other Systems Via Messages - Struct Member Manipulation

Dec 5, 2012

I work on a system that communicates with other systems via messages. Those messages are defined in a spec and every word must be exactly as defined. To accomplish this Ada allows me to define the fields of my record to specific words in memory.

Code:
word = 4;
type Msg_Type1 is record
x: Some_Type;
y: Another_Type;
z: Another_Type;
end record;

[Code] ....

View 5 Replies View Related

C++ :: Creating 2D Array Within Class Using Constant?

Oct 17, 2014

I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.

class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};

View 4 Replies View Related

C++ :: Static Constant Datatype For Fraction Class

Aug 4, 2013

My Fraction.h class looks like :

class Fraction {
int num;
unsigned int den;
public:
Fraction(int = 1,int =1);
//Constants of Datatype

[Code] ....

The implementation Fraction.cpp is as follows :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d){
cout << This is double param constructor <<endl;
}

And the application main.cpp is

int main(){
Fraction f1(3,9);
f1 = Fraction::sc_fUnity; // how to implement this ?
}

How can I write the Fraction.cpp for the constant static member ?

View 6 Replies View Related







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