C++ :: Typedef Template With Static Const Member?
Apr 18, 2013
class Tracker {
public:
static const int type;
typedef cv_types::CvType<type>::type_t type_t;
};
const int Tracker::type = 1;
gives me the error:
'I' : invalid template argument for 'cv_types::CvType', expected compile-time constant expression
Shouldn't the static const int be a compile time constant?
How would I specify it, so that it works?
PS.: The code works with #define type 1 at the top of the file and without the static const int.
View 3 Replies
ADVERTISEMENT
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
Dec 7, 2013
difference between const and static const, more effectively. I know the basic concept of const and static but I need clear explanation of declaring "const" and "static const"
Ex:
const int a;
static const int a;
View 5 Replies
View Related
Oct 7, 2014
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
View 1 Replies
View Related
Sep 11, 2013
What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?
View 7 Replies
View Related
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
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
Jul 19, 2013
I have a class "Result" with a single template function Set(const std::string& arName, T& val) and a specialization of this function Set<Real>(const std::string& arName, Real& val) where Real is a typedef for double. The class is in a shared library and I use it in my main program. If I do result->Set<GLOBAL::Real>("U", 100.0); the wrong template function is called!
I check this by the output with std::cout.
Maybe it's a problem with the typedef.
If I link the object file of the Result class directly to my main program (no shared library), it works.
typedefs.hpp:
namespace GLOBAL {
typedef double Real;
} results.hpp
#include <iostream>
[Code] ....
View 3 Replies
View Related
Apr 11, 2014
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 2 Replies
View Related
Sep 27, 2014
i am trying to describe the unusual situation where you declare a class member function with this format:
bool class::function_name(void) const
Specifically where the 'const' follows the parameter list. It is my understanding this is a very useful way of ensuring that whatever code you put in the function definition cannot change any data members of its class.
However I have recently read that this form of declaration should not be used as it leads to less optimized and slower code. Is this correct?
View 3 Replies
View Related
Jan 24, 2013
Code:
class Editor {
// c'tors etc
Gtk::EventBox canvas_event_box;
void functionA();
void functionB() const;
[Code] ....
When I try to compile functionB in Visual C++ it gives me this error:-
glibmm/refptr.h(199) : error C2440: 'initializing' : cannot convert from 'const Gdk::Window *' to 'Gdk::Window *'
Conversion loses qualifiers
And this is the code from glibmm/refptr.h
Code:
// The templated ctor allows copy construction from any object that's castable. Thus, it does downcasts:
// base_ref = derived_ref
template <class T_CppObject>
template <class T_CastFrom>
[Code] .....
I don't actually want to change anything in the member variable canvas_event_box. I just want to be able to call one of its functions from my 'const' member function. Is there any syntax I can use to tell VC++ that I'm not actually changing the variable - just using it.
View 11 Replies
View Related
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
May 27, 2014
i want to store reference to a const object in my class as a member variable, as follow:
I basically want a readonly reference to |Data| in Device object.
Code:
class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}
This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!
What is the alternative? Is pointer the nearest? which of the following should be used?
Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????
View 7 Replies
View Related
Oct 17, 2014
My code is here [URL]
void Player::Display() const
{
cout << "
Player Name: " << GetName() <<
"
Grade: " << GetGrade() << "
G.P.A. " << GetGPA() << endl;
}
The problem occurs in here, I get access violations, is there a way to this while keeping Display const or is this code valid and my problem is somewhere else and not being caught in the debugger? I tried to make the return types const - but that didn't work .....
//Getters need const twice for this to work?
const char* Player::GetName() const {return m_name;}
const int Player::GetGrade() const {return m_grade;}
const double Player::GetGPA() const {return m_gpa;}
[Code].....
View 2 Replies
View Related
Jul 24, 2014
Here I'm trying to initialize PersonFactory::ethnicSurnames just once for the entire run of the program:
#include <iostream>
#include <string>
#include <vector>
#include <array>
enum Country {India, China, France, NumCountries}; // plus many other countries
struct School {}; struct Mall {}; struct HockeyArena {};
[Code] ....
Output:
PersonFactory::initializeEthnicNames() called
Carrying out the initialization...
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
numberOfTimesInitialized = 1
As you can see, even though five PersonFactory objects were constructed, the ethnicNames initialization only occurred once, as desired. However, there are some issues with my method. First of all, the use of the comma operator is ugly in my opinion. But fashion statements aside, PersonFactory::initializeEthnicNames() is still called multiple times, which is not good, even though it correctly avoids reinitializing ethnicNames after the first call. Also, I now forever get the annoying compiler warnings that the bool namesInitialized is never used, which is true, thus wasting a small bit of memory. And finally, I cannot declare ethnicNames const now, and it is supposed to be const. Any better way to accomplish what I'm trying to do?
By the way, the reason why I don't initialize ethnic names outside the class as is normally done for static data members (and that would indeed allow me to declare it const) is because it would get messed up if I later change the order of the elements in enum Country. Hence actual lines of initializations I think are needed. And I do want ethnicSurnames inside PersonFactory, because I feel it really does belong there. Also, PersonFactory is not to be Singleton, because it has data members that depend on some parameters in its constructor (e.g. geographic location).
View 5 Replies
View Related
Jul 18, 2012
I try to create small project in order to better understand how key word static works with templates . However some compiles errors crush my plan.
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> main.cpp
1>c:all myс++ha level 6solution level 6solution level 64.2b - ex1. static variable for array def size. templatesarray.cpp(40): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
[Code] .....
View 7 Replies
View Related
Sep 6, 2013
Can people just do a quick sanity check of this concept?
I'm playing around with a technique to parameterise a template with the largest 'sizeof' result for a set of classes.
A, B, C & D are classes with 1, 2, 3 & 4 members respectively.
e is an instance of E, who's array member is sized according to the largest size of A, B, C or D.
Code:
// MaxSizeOf for four parameters.
template <const size_t S1,
const size_t S2,
const size_t S3 = 0,
const size_t S4 = 0>
struct MaxSizeOf
[code].....
View 11 Replies
View Related
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
May 25, 2014
I am getting this error invalid use of non static data member.my code looks something like this: i have a main.cpp and 2 class files with respective .h files, say one class file is named human (so i have human.cpp and human.h) and stats (so i have stats.cpp and stats.h) in my stats.h file, i have a double array: double HumanF[10][12] with everything filled in.then in my human.h file i just have a bunch of integers. human.cpp has formulas in it that use numbers from the double array i mentioned. for example
Human::Human() {
constant (this is a double i made in human.h) = (1+Stats::HumanF[0][0]);
i (another double) = pow(constant, ylvl);
(ylvl is also an int I made in my header file)
yhp = i*137;
}
View 11 Replies
View Related
Jan 19, 2014
How does one use a functor as a static constexpr member? I had this basic functor for a class:
struct functor{
short operator()(char c)const{return c-'0';}
};
And in the class, I use it as a static constexpr member:
class Foo{
public:
//...
private:
static constexpr functor k_funky = functor();
};
During the linking stage, I kept getting "undefined reference to 'Foo::k_funky'". So then I tried declaring the functor's constructor and operator function constexpr:
struct functor{
constexpr short operator()(char c){return c-'0';}
constexpr functor() = default; //This counts, doesn't it?
};
But I received the same error.
View 4 Replies
View Related
Aug 27, 2014
I need to keep a static variable in a member function of a class that I have many objects of. I've had some trouble with it, and when I read up I found that such variables are static across all instances. Is there any way around this?
View 3 Replies
View Related
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
Mar 8, 2014
What am I doing wrong with static members and methods here?
compiler errors:
1>test.obj : error LNK2005: "private: static int Test::count" (?count@Test@@0HA) already defined in main.obj
1>c:usersjamesdocumentsvisual studio 2013Projectsstatic_testReleasestatic_test.exe : fatal error LNK1169: one or more multiply defined symbols found
test.h
#ifndef TEST_H_
#define TEST_H_
class Test {
[code]....
View 4 Replies
View Related
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
Aug 26, 2014
I need a static hash table to keep track of all objects of a particular type that are instantiated in a Qt application but I have never used a template class as a static member object before and I can't seem to figure out how to initialize it. QHash is the hash table class that follows the template convetion:
template<class key, class data>
QString is probably self explanatory.
Example Header:
class MyClass {
...
private:
static QHash<QString, MyClass*> instanceTable;
}
Here is my source that doesn't compile.
Example Source
#include header.h
// using default constructor for table...
QHash<QString key, MyClass* instance> MyClass::instanceTable(); // gives Error below.
// Error in above line is "Declaration is incompatible with QHash<QString, Myclass*>"
I have tried doing it a number of different ways and none of them work. How do you initialize a static template object?
View 2 Replies
View Related
Nov 29, 2012
The below code compiles without error using VS 2012 but with g++ 4.1.2 I get this error:
Code:
main.cpp: In function 'int main(int, char**)':
main.cpp:37: error: no matching function for call to 'StringHelper::stringToNumeric(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Here is the code:
#include <string>
#include "boost/lexical_cast.hpp"
using boost::lexical_cast;
using boost::bad_lexical_cast;
class StringHelper {
[Code] ....
This is part of a larger program so in reality StringHelper has more static functions but this example does produce the error the same as the code when in the larger program.
To get it to compile under g++ I had to assign the return value from substr() to a string and pass that string to stringToNumeric. Why do I have to do this for g++ and not VS? Do I have something wrong with my template function that g++ is calling out and VS is not?
View 4 Replies
View Related