Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have static member function inside my class which I would like to add in my namespace.
class A{
public:
static void func();
};
namespace myNamespace{
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
I get the following errors:
error: definition of ‘void A::func()’ is not in namespace enclosing ‘A’ [-fpermissive]
error: ‘myNamespace::A’ has not been declared
myNamespace::A::func();
void A::func(){
what is the reason?
because you put a function that belongs to the class A inside namespace. just make the class and function A together inside namespace your code should be like this
namespace myNamespace {
class A {
public:
static void func();
};
void A::func() {
}
}
int main()
{
myNamespace::A::func;
return 0;
}
The declaration and the definition both should be inside the NameSpace for it to work. Here in your code, you have declared the class outside the namespace. You can fix it as follows.
namespace myNamespace{
class A{
public:
static void func();
};
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
Also try to understand the error messages which would save you a lot of time googling.
https://www.crashhandler.com/ ---> Best Practices in C++ {EDIT :: A blog by myself}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
#include <iostream>
using namespace std ;
class maths{
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}
In C++ class members default to private. Private members can only be accessed from within the class itself. You need to set the members to be public, like this:
class maths {
public:
// Everything after this label is now public
int a;
int b;
// etc.
// If you want to define some members as private later, do this
private:
int a_private_variable;
};
One way to solve this would be to just replace the keyword class with struct as shown below.The use of struct makes all the member of the class public by default which means the user of the class can access them directly.
#include <iostream>
using namespace std ;
struct maths{//NOTE THE KEYWORD STRUCT HERE
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm writing in C++ and my code is getting bigger and bigger. I wonder if I could somehow move code fragment to a different file. Let's say i have:
class First {
class Second
{
void a();
void b();
};
void c();
};
I would like to move my Second class to a Second.h file and then of course write bodies of Second class and functions into Second.cpp file. How can I do this?
Given that class Second is nested in class First you could start by refactoring your code like so:
class First
{
class Second;
void c();
};
class First::Second
{
void a();
void b();
};
From there it is easy to break the two classes into separate files:
First.h:
class First
{
class Second;
void c();
};
And Second.h
#include "First.h"
class First::Second
{
void a();
void b();
};
And then your .cpp files would be:
First.cpp
#include "First.h"
void First::c()
{
}
Second.cpp
#include "Second.h"
void First::Second::a()
{
}
void First::Second::b()
{
}
Lets assume you have First.h
class First{
class Second
{
void a();
void b();
};
void c();
};
you can provide the implementation of class Second in a separate translation unit:
like
First.cpp:
#include "First.h"
void First::c() {
}
Second.cpp:
#include "First.h"
void First::Second::a() {
}
void First::Second::b() {
}
I'm not sure that's what you're after though.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that templates must be declared and defined in the same file. But, why I cannot:
#ifndef guard
#define guard
template<typename T>
class Test{
void method(){
}
};
#endif
And it is causes compiler error ( not directly but instatantiation template Test in two different place- for example in main() and as field in any class causes error.
It must be defined outside class ( It doesn't cause error like here)
#ifndef guard
#define guard
template<typename T>
class Test{
void method();
};
#endif
template<typename T>
void Test<T>::method(){}
Why?
Member functions of class templates can be defined inline, inside the class template declaration.
The code in the question:
template<typename T>
class Test{
void method(){
}
};
is well formed. Asking why you can't do it is misguided.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
class Abc // created class with name Abc
{
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
int main()
{
Abc obj;//creating object of the class
obj.display();//calling member function inside class
}
It returns error as
main.cpp: In function 'int main()':
main.cpp:5:10: error: 'void Abc::display()' is private
void display()
^
main.cpp:13:17: error: within this context
obj.display();
^
I have tried to make display function public int main but then it gives error as
main.cpp:5:11: error: expected ':' before 'void'
public void display()
^
Declare it as:
class Abc
{
public:
void display()
{
cout<<"Displaying NO."<<endl;
}
};
or:
struct Abc
{
void display()
{
cout<<"Displaying NO."<<endl;
}
};
struct's default protection is public, class's default protection is private
The error message is clear enough. You may not call a private member function outside the class. By default classes with keyword class have private access control by default. Either write
class Abc // created class with name Abc
{
public:
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
or
struct Abc // created class with name Abc
{
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
Classes with keyword struct have public access control by default.
Also it would be better to define the member function as
void display() const//member function
{
std::cout<<"Displaying NO."<<endl;
}
Take into account that as you did not use directibe
using namespace std;
you have to use qualified names with entities declared in std::. For example
std::cout<<"Displaying NO."<<std::endl;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I want to do now is to make changements in FUNC.cpp and FUNC.h to inherit it to main.cpp and then, generate a diagram class at the end in which is really states that FUNC is inherited.
I want to make changements in my code, from namespace to classes, to allow the inheritance process.
I ' m having what is follow:
In a FUNC.h:
namespace FUNC
{
void f1(...);
void f2(...);
}
In a FUNC.cpp
namespace FUNC
{
void f1(...)
{
}
void f2(...)
{
}
}
in test.cpp (which is meantime a class an having its test.h) , it's possible to call f1 and f2 as follow:
FUNC::f1(...);
FUNC::f2(...);
If you change the namespace to a class, but still want to call func1 and func2 using the same syntax (e.g. FUNC::func1()), you have to make the functions static:
struct FUNC
{
static void func1();
static void func2();
};
If you want to override func1 in an inherited class then it's simple:
struct FUNC2 : public FUNC
{
static void func1();
};
There are however problems with static member functions, in that they can't access non-static members easily.
I will answer my own question
I had to insert after
class FUNC: public TEST
{
static void func1();
static void func2();
};