Quick question: how can I initialize this? The syntax isn't working.
#include <iostream>
using namespace std;
template<typename TYPE>
class Heap1 {
class Node {
public:
friend Heap1;
private:
TYPE elt;
Node *child;
}; // Node
};
int main() {
Heap1<int>.Node var;
return 0; }
I'm reading this answer but the syntax isn't too clear to me: Creating instance of nested class
Heap1<int>.Node var;
The syntax isn't working
Try
Heap1<int>::Node var;
But actually, you can't. Heap1<>::Node is private and thus inaccessible from the outside world.
Related
I've seen a few answers on this topic, but to my knowledge they don't quite answer the problem I'm getting. Here is my paraphrased code:
using namespace std;
class classname
{
private:
int foo;
classname();
};
classname::classname()
{
//dostuff
}
int main(){
classname arr[5];
}
The code errors:
error: ‘classname::classname()’ is private within this context
My best guess is that private: bled into my constructor definition. How do I stop this?
Everything after private: is marked as private until the end of the class is reached or another visibility token. Put your function above the private: or put a public: before the function declaration.
I|m having problems understanding the scope of a class or a function. This program is incomplete but I am not being able to use a function within the same class and then from a different class. For example: I get an error that says
"'selector' was not declared in this scope"
Can you help me figure out what's wrong? Thanks
#include <iostream>
using namespace std;
int main(void){
selector();
}
void selector(){
linkedList test;
/* block of code */
}
class linkedList{
Node *head;
public:
linkedList(){
head = NULL;
}
//other lines
};
class Node{
public:
int data;
Node * next;
}
I don't understand why you're talking about classes, but the scope of a function is from its declaration to the end of the file. Just swap the two functions in your code:
void selector() {
// linkedList test;
/* block of code */
}
int main() {
selector(); // selector is in scope here
}
(I'm not sure why you're doing int main(void) either. That's more of a C thing. A C++ function with no arguments looks like int main().)
I have a queue.h file like the following.
Is it possible that I can access the head pointer of the queue from Main?
If yes, what should I do in main?
Since the head pointer is a class pointer, and its type is a protected nested class, I don't think I can access it from main.
Therefore, I try to create a function getHead() as public member. However, another problem comes, it is I am using template class. Please guide me how to solve this problem.
my header file:
#include <iostream>
#include <iomanip>
using namespace std;
class PCB
{
public:
int PID;
string fileName;
};
template<class T>
class myQueue
{
protected:
class Node
{
public:
T info;
Node *next;
Node *prev;
};
Node *head;
Node *tail;
int count;
public:
void getHead(Node **tempHead);
};
template<class T>
void myQueue<T>::getHead(Node **tempHead)
{
*tempHead = head;
}
#endif
my main is:
#include "myQueue.h"
#include <iostream>
int main()
{
myQueue<PCB> queue;
//How can I access the Head pointer of my Queue here?
//queue.getHead(&tempHead);
return 0;
}
To acess myQueue::Node from outside the class you need to rewrite your getter function a bit:
template<class T>
myQueue<T>::Node* myQueue<T>::getHead()
{
return head;
}
Then you can use it in main() like this
auto head = queue.getHead();
Note that the usage of auto is important in this case. You still cannot declare any variable of type myQueue<T>::Node or myQueue<T>::Node** outside of myQueue<T>, but you can use auto variables to hold these types.
I have a template class that when I instantiate in main doesn't have any issues but when i try instantiating the same in another class is giving issues. can someone please enlighten me on the solution for this
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
main()
{
property<int> myIntProperty("myIntProperty");
}
the above code compiles with out any issue.
but
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
class propertyHolder
{
property<int> myIntProperty("myIntProperty");
};
this code is not getting compiled.
giving me error like
main.cpp|19|error: expected identifier before string constant|
main.cpp|19|error: expected ',' or '...' before string constant|
Thanks,
Harish
property<int> myIntProperty("myIntProperty");
This is a function declaration, so it expects you to insert a default argument after identifying it, like string s = "myIntProperty".
Perhaps you want to initialize an object called myIntProperty,
property<int> myIntProperty {"myIntProperty"};
This can be done in C++11, but you can also initialize it in the constructor initializer list,
// Header
class propertyHolder {
public:
propertyHolder( string s );
private:
property<int> myIntProperty;
};
// Source
propertyHolder::propertyHolder( string s ) :
myIntProperty( s )
{
}
You wanted to declare field in class propertyHandler. That syntax is not working because you cannot declare a field and assing it value at the same spot.
You can delcare it, and initialise in constructor:
property<int> myIntProperty;
propertyHolder(): myIntProperty("name") {}
or with c++11 syntax:
property<int> myIntProperty{"name"};
or declare it static, and them declare like that:
static property<int> myIntProperty;
and after class declaration:
property<int> propertyHolder::myIntProperty("name");
If the class G is in the namespace GSpace and it needs to be friends with the class M in the global namespace what do you have to do? I thought this would work:
/////////////////////M.h//////////////////////
#include "GFile.h"
class M
{
public:
friend class GSpace::G; // doesn't work, throws error
}
After researching on StackOverflow a bit, I found this answer https://stackoverflow.com/a/3843743/1797424
/////////////////////M.h//////////////////////
namespace GSpace
{
class G;
}
class M
{
public:
friend class GSpace::G; // works, no error
private:
GSpace::G gClassMember; // errors: "M uses undefined class GSpace::G"
};
// Note that G.h includes M.h so I do not include it here,
// instead I have it included in M.cpp
That does work for getting the class friended. However, it creates an issue when I actually declare a class member using that type, because the class is not defined. GFile.h
Am I misunderstanding how #include and forward declaration behaves, or is it some kind of implementation issue on the side of the compiler (not likely, I'm assuming)?
Because your member is not a pointer or reference, the compiler needs to know the size of G. You can't use a forward declaration.
As noted in the comment, you need to qualify G with the namespace.
Here is code which compiles for me:
namespace GSpace
{
class G
{
};
}
class M
{
public:
friend class GSpace::G;
private:
GSpace::G gClassMember;
};
int main() {return 0;}
Try the following
namespace GSpace
{
class G;
}
class M
{
public:
friend class GSpace::G;
}
namespace GSpace
{
class G { /* definition of the class */ };
}