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.
Related
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.
I'm having a compilation error using Apple's Clang 7.0 with the following friendship code, using C++11 standard. I wonder what's really wrong with it since it seems to be valid for me. I'm describing the setting and the error I'm having:
MyInterface
namespace namespace1
{
class MyInterface
{
friend class MyClass;
public:
virtual void some_method(void) = 0;
...
private:
type some_attribute;
...
}
}
MyClass::MyMethod Implementation
namespace namespace2
{
void MyClass::MyMethod(MyInterface* MyConcrete)
{
...
// MyConcrete implements MyInterface
if(MyConcrete->some_attribute == some_value) // Error*
{
...
}
...
}
}
Error
error: 'some_attribute' is a private member of 'namespace1::MyInterface'
I really expected that MyClass would have access to some_attribute in MyConcrete (which implemented MyInterface) regardless of the class access modifier. Any clues why this error is happening? Any suggestions?
Thank you!
MyClass is in namespace2. So you need to use:
friend class namespace2::MyClass;
You may also need to use forward decleration of MyClass before you define MyInterface.
Here's an example that compiles:
// forward decleration
namespace namespace2
{
class MyClass;
}
namespace namespace1
{
class MyInterface
{
friend class namespace2::MyClass; // added missing namespace
public:
virtual void some_method(void) = 0;
private:
int some_attribute;
};
}
namespace namespace2
{
class MyClass
{
void MyMethod(namespace1::MyInterface* MyConcrete)
{
if(MyConcrete->some_attribute == 1)
{
}
}
};
}
int main()
{
}
You can run it here.
friend class MyClass; in the context of ::namespace1::MyInterface refers to the class ::namespace1::MyClass, which is a different class from ::namespace2::MyClass. (That's the whole point of namespaces, right?)
Change the friend declaration to read like this:
friend class ::namespace2::MyClass;
Note that this requires that the type ::namespace2::MyClass has already been declared, so you either need to forward-declare it (namespace namespace2 { class MyClass; }) or you need to make sure that the definition is included prior to the definition of ::namespace1::MyInterface.
(See this demo.)
My check.h file has an enum as a private variable. I do not know what to do to declare enum as a global entity.
I declare a function in check.cpp with enum return type. But it is giving the error as follows.
Multiple markers at this line
Type 'TStatus' could not be resolved
‘TStatus’ does not name a type
Member declaration not found
my program is as follows. Can anyone please give me a solution for this.
check.cpp
#include <iostream>
#include "check.h"
using namespace std;
check::check() { }
TStatus check::getStatus()
{
......
}
check::~check() { }
check.h
#ifndef CHECK_H_
#define CHECK_H_
class check {
private:
enum TStatus { ok,sold,defect };
public:
check();
~check();
TStatus getStatus();
};
#endif /* CHECK_H_ */
Firstly, you just need to write
check::TStatus check::getStatus()
^^^^^^^
because otherwise the compiler does not know where does TStatus come from.
See full compilable code here: http://ideone.com/l5qxbK
But note also another problem. Your getStatus() is a public function, so you will probably want to call it from outside of a class. But you will not be able to do this because the return type is private, and thus can not be used outside of the class (bar note below). So you will either need to make the enum public, or you can make your getStatus() private if it is not used outside of a class.
Note: you can in fact use getStatus() outside of your class even is your TStatus is private — if you do not use the getStatus() result; see my ideone code linked above. Though in sensible design such call should not have much sense.
Also, see Vlad's answer on how you can use auto to actually store the getStatus() results even is TStatus is private. Though still better is to make TStatus public.
You have to specify class name before name TStatus
check::TStatus check::getStatus()
//...
Here is a demonstrative program
#include <iostream>
class check {
private:
enum TStatus { ok, sold, defect };
public:
check() = default;
~check() = default;
TStatus getStatus() const;
};
check::TStatus check::getStatus() const { return defect; }
int main()
{
auto status = check().getStatus();
std::cout << status << std::endl;
}
The program output is
2
You have to change your function definition as follows:
check::TStatus check::getStatus()
{
return sold;
}
Demo:http://coliru.stacked-crooked.com/a/0ca5333f3674d39b
I have two classes, Friend2 is a friend of Friend1. After Friend2 accesses the Friend1's private member variable, I want Friend1 to be able to access the Friend2's public member functions. So I decided to use composition inside Friend1. However, the compiler shows me the error:
use of undefined type 'friend2'
Then, I tried another way, making Friend1 a friend of Friend2 too. But I still got the same error. Would anyone teach me the way to solve? Thx a lot!
#ifndef FRIEND1_H
#define FRIEND1_H
class friend2;
class friend1 {
private:
int x;
public:
friend1();
int comp(friend2* f2o);
friend class friend2;
};
friend1::friend1() {
x = 1;
}
int friend1::comp(friend2* f2o) {
return f2o->getxx(); //the error : use of undefined type
}
#endif
#ifndef FRIEND2_H
#define FRIEND2_H
#include "friend1.h"
#include <iostream>
class friend2 {
private:
int xx;
public:
friend2();
void p(friend1* f1o);
int getxx() const;
friend class friend1;
};
friend2::friend2() {}
void friend2::p(friend1* f1o) {
xx = f1o->x;
}
int friend2::getxx() const {
return xx;
}
#endif
Also, is composition or friend class the better way to do this? Why?
You get //the error : use of undefined type because class Friend2 is only declared, not defined at that point. To solve this move int friend1::comp(friend2* f2o) implementation to friend1.cpp and include friend2.h from there.
UPDATE In general, if two classes are mutual friends (and even if only one of them is a friend to another), it's a good reason to think about the design.
I am actually testing a file and I have a situation, where I need to access some of the protected members of the class from main.cpp. I tried to add, main() as friend, didn't work out and learned that it wont work, so I moved everything in the main() to a test () and made the test() as friend. still it shows the error.
Example would be
//--File.hpp
namespace Files {
class File {
public:
File(long word_):word(word_) {}
protected:
long word;
private:
friend int test();
};
}//ns:Files
//--List_File.hpp
namespace Files {
class List_File :public File {
public:
List_File() : File(sizeof(int) + sizeof(long)) {}
private:
friend int test();
};
}//ns:Files
//--main.cpp
using namespace Files;
int test() {
File *pd = new List_File();
assert(pd->word == 12); //LINE 34
return 0;
}
int main() {
test();
return 0;
}
//it says error on Line 34: Base::value is protected. Please advice.
g++ -O -Wall -Wno-unused -o a.out File.cpp List_File.cpp Data_File.cpp
Free_List_File.cpp main.cpp
File.hpp: In function ‘int test()’:
File.hpp:30:7: error: ‘long int Files::File::word’ is protected
main.cpp:34:16: error: within this context
make: *** [a.out] Error 1
No, it definitely doesn't have to be in the same file, but it obviously has to "know" what the class is, i.e.: the header that has the class definition should be included in the file where the function is implemented. Your code should be fine, as commented.
after you added some context
Your test function is not in the Files namespace. If you want it to be in the global context, you should treat it as "::test" within the namespace, otherwise the compiler might expect the "Files::test" to be the friend function, and not the "::test" as in your case. Can't find the formal standard reference, but I'm pretty sure that's the case. Note that you're performing a forward declaration here, so there's no default fall-back to the upper level of scope for name resolution.
Maybe you missing inheritance declaration in Derived class?
class Derived : public Base {
I've tested your code (with inheritance declaration included) and it produced no error
littedev is right!!
Updated the code according to the comments by littedev..
//--File.hpp
namespace Files {
class File {
public:
File(long word_):word(word_) {}
protected:
long word;
private:
friend int test();
};
}//ns:Files
//--List_File.hpp
namespace Files {
class List_File :public File {
public:
List_File() : File(sizeof(int) + sizeof(long)) {} \
private:
friend int test();
};
}//ns:Files
//--main.cpp
namespace Files{
int test() {
File *pd = new List_File();
assert(pd->word == 12); //LINE 34
return 0;
}
int main() {
Files::test();
return 0;
}
}
I would guess that you are trying to access a protected variable outside of the scope of the Files class definition. I would recommend a function that returns the variable word when it is called and use that instead of trying to access a protected variable outside of a class definition. I could be wrong in that I am not really sure what is the scope of a protected variable (whether it is limited only to class declarations or whether it can be accessed outside of the class definition) but I am pretty sure that I am right because protected variables are like private variables. They are only accessible within the class scope. Correct me if I am wrong.
EDIT: Oh I am sorry I didn't realize what you were doing. littleadv is right, your function declaration isn't within the files namespace.