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?
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
#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 .
I am work on building a simple parse tree and the layout of my code look like this:
Headers pt_node.hiterator.hparsetree.h
Source files node.cppparsetree.cppmain.cpp
I am still relatively new to C++ , and have been advised to include function definition for the member function of both pt_node class and iterator class in the node.cpp file
I particular I have declare the following iterator.h:
#include "stdafx.h" #include <iostream> #include <math.h> using namespace std; class Calc {
[Code] ....
when i built it, it showed the following errors:
1>------ Build started: Project: rough, Configuration: Debug Win32 ------ 1> rough.cpp 1>e:c programs ough ough ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier 1>e:c programs
The question is: Define the class Counter. An instance of this class is used to count things, but the counter should never be less than 0 (non negative number). The member variable should be private. I realize what I'm suppose to be using but can't implement the member functions needed..
int main(){ int value; cin >> value; Counter myCounter(value); for (int i = 1; i <= MAXLOOP; i++) { myCounter.increment();
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
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;
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.
I'm redesigning some code and I'm wondering what the best ways to store and access certain data is. The characteristics are as followed:
1) Based on data from a file, a distance matrix (N x N) is calculated. 2) Once the matrix is complete, the data will never change (unless a new file is read, but I can work around that by iteratively calling the problem with a new datafile on as command line parameter). 3) The data from the matrix is accessed billions of times from pretty much every other line of code.
In my old version, I had a class "Data" which a sub-class "Data::Distance" and I would put a reference in every other class that needed it. Now, my class hierarchy will be much flatter (basically all logic will be in one class; other classes will be POD structs).
Given the characteristics of the Distance table, is there a way to store them in a very efficiently-accessible way? Does it matter if it's stored in the main class where all the action happens in contrast to being a different class? Does making it static improve the performance? Casting it to const? Anything?
Again, the data is accessed billions of times so even minor differences can save a lot of time.
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; }
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
So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.
Find my code below:
#ifndef PLAYER_H #define PLAYER_H #include "SDL.h" #include "SDL_image.h" #include "CTexture.h" class Player {
I recently discovered the new - new to me anyway! - feature of modern C++ that allows you to set the initial value of a data member when you declare it:
class CINTWrapper{ private: int m_iData=0; };
This even extends to calling member functions that work with initialization I believe:
class CStringWrapper{ private: wchar_t* Allocate_Array(const int iBufferSize); wchar_t* m_pString=Allocate_Array(1); };
At first, this seemed an extremely useful piece of functionality that C++ had been lacking all along. However, the more I thought about it the more it struck me this feature actually undermines one of the principle design elements of the language - that being the Constructor.
As I understand it the primary purpose of the Constructor is specifically to give the programmer a place where it is guaranteed he can always initialize his data members before anything else is done with the class. However, given the new initialization rules this is no longer necessary. So it largely seems to me that Constructors as a whole are no longer necessary either! Copy-Constructors are a special and vital case. Admittedly when I was using them for their intended purpose I hated either the redundancy you had to introduce across multiple Constructors; those with and without arguments and so on, or alternately the fine tuning of helper-functions to do common initialization between these variants. Now however I sort of regret this cast-iron rule has been taken away.
As a last point, I am trying to change the way I think about programming. I am trying to employ more objects than pure C-style ('int' or 'double', etc) data types and especially to move into templates (although absolutely NOT the Hewlett Packard template library!). Given my current understanding of inheritance in particular it seems to me that using pre-initialized data members rather than Constructor-initialization makes object derivation even more complicated, not less so.