So I'm writing a small program for class, and for some reason I keep getting an error when trying to initialize head to NULL. Even threw in the namespace just to see, nothin'.
#ifndef NUMBERLIST_H #define NUMBERLIST_H using namespace std;
[Code].....
There's my header file. Not sure what I'm doing wrong with the constructor.
EDIT: Got it to work with nullptr, but still curious why that isn't working
Let's say I have a Car object , and it contains inner Engine object.
Code: struct Car{ Engine mEngine; };
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code: Car::Car: mEngin(arg1,arg2,...) { other stuff here }
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code: class Toyota: Car(...), mEngine(...), mGear(..) { ... };
The other options are: 1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive 2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock. Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock { friend class SeqBuildBlockIRns; public: MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)// more arguements in this constructor of derived class : SeqBuildBlock (pSBBList0)
[code]....
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2' MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
I am working on a homework assignment and have most of the program working, but when I try to compile it keeps telling me to initialize the coin variables in each class. However, they are supposed to be added then removed so I don't want to set them back to zero.
Rewrite the Purse program given in Page 35 with functions to perform insert and remove operations. The function insert (int p, int n, int d, int q) will initialize pennies, nickels, dimes and quarters. The function dollars() will return the dollars. The function remove (int p, int n, int d, int q) will subtract pennies, nickels, dimes and quarters. The function display() returns a new String to print the content of the purse with remaining pennies, nickels, dimes and quarters.
Code: usingnamespace std; int insert_money (int *p, int *n, int *d, int *q); int remove_money (int *p, int *n, int *d, int *q); int dollars(); int main()
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
I want to use this array as part of my class. I have tried several different angles trying to get it to work but with out success. I have been checking to see if it works by simply using "cout << dayName[3];" It is printing nothing at all. What is the proper way to initialize this array of strings?
First I tried this: const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:
//PARENT.H class Parent { protected: static int n; static std::map<int, Parent*> map; public: virtual void pureVirtual() = 0;
[code]....
The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.
The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.
class Base { char * ptr; public: Base(){} Base(char * str)
[code].....
Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.
Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in both the classes are private.
I tried using initializer list but could not succeed.
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
My program is a basic MFC AppWizard (exe) created project in VC++ 6. In MainFrm.cpp, I am trying to access some user defined CMyView member functions. However when I try to do the standard procedure to get the CMyView pointer in MainFrm.cpp, I get the " ... 'CMyView' : undeclared identifier" error. To resolve this, I add " #include myView.h " at the top of MainFrm.h which then produces the following errors:
Code: myview.h(21) : error C2143: syntax error : missing ';' before '*' myview.h(21) : error C2501: 'CMyDoc' : missing storage-class or type specifiers myview.h(21) : error C2501: 'GetDocument' : missing storage-class or type specifiers
What do these errors mean? Is there a simple way to access CMyView member functions from CMainFrame?
I'm trying to implement a class hierarchy and a wrapper class with a pointer to the base class. The base class has operator< overloaded and the implementation makes use of virtual functions as some of the logic for sorting is in the derived classes. Unfortunately, when trying to use the base class operator< from the wrapper, I get a "pure virtual method called".
Below code is meant to illustrate my problem. Unfortunately it crashes on me (upon destruction of vec) and I cannot quite see, why. So two questions:
1. spot the error I made in the code below (having lived in Java-land for the last 5 years, I'm sure I just did some stupid error)?
2. How can I implement Wrapper::operator< to use Base::operator<? I know I could write a function and pass it to sort but I'm interessted if there is a way to actually use Base::operator<.
I have encountered a problem I can't see to solve. I want to access a function and can't seem to find the right combination to get me there. Here is what I am looking at:
CFoo1::CFoo2::GetStrDataC(int nRow) const
How do I call the GetStrDataC function from another class?
A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?
If I need a static pointer to a class that is used globally(multiple files), and I only want to allocate memory once.
One way is to create a function that returns a static pointer of type class and call it where ever you need this pointer. My question is there another way to do this like with a header file and include the header file where you need to use the object of type class.
static class* function { static class c; if (c == NULL) { c = new class; } return c }
#include <iostream> #include <vector> #include <string> #include <map> using namespace std; struct bop { string realname; //real name
[Code] ....
Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?
I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.
If i have a pointer array of classes, e.g. MyClass *foo; and if i try to delete this pointer delete [] foo; does it call a destructor of a class, or wahat happens? this is because i have another pointers in that class which are cleared in destructor.
I want to ask about pointers to classes vs just classes. I have used classes such as QTimer with and without pointers, and I don't know when to use pointers. Sometimes you can't because a function returns a class, not a pointer to a class. But usually you can do either.
Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?
1 #include<iostream> 2 using namespace std; 3 4 class A { 5 public: 6 ~A() { 7 cout<< "~A()" <<endl;