class Rectangle {
int x, y;
public:
void set_values (int,int);
int area (void) {return (x*y);}
};
void Rectangle::set_values (int a, int b) {
x = a;
y = b;
}
I have this class inside of function of another class
its giving error: a function-definition is not allowed here before ‘{’ token
could you say me why?
You can't have a write a function definition inside another function in C++. If anything, you'll need to write the implementation inside your class declaration, like you did with the area function.
You should separe your declaration (.h) from your implementation (.cpp). If you want to implement some function in your declaration file (nomally for simple functions) you should use the inline reserved word:
Rectangle.h
class Rectangle {
int x, y;
public:
void set_values (int,int);
inline int area (void) {return (x*y);}
};
Rectangle.cpp
#include Rectangle.h
void Rectangle::set_values (int a, int b) {
x = a;
y = b;
}
You can make a type in function scope, but you can't declare the function there. You can do this:
class Rectangle {
int x, y;
public:
void set_values (int a, int b) { x = a; y = b; }
int area (void) { return (x*y); }
};
But, why not just declare Rectangle normally? It seems useful enough to want to use in other functions.
Related
I am using CodeBlocks.
In C++, I have 3 header files and 3 cpp files like below.
Base.h
class Base
{
public:
virtual int funky(int x, int y);
};
Base.cpp
int funky(int x, int y) {
return x+y;
}
FirstClass.h
class FirstClass: public Base
{
public:
virtual int funky(int x, int y);
};
FirstClass.cpp
int funky(int x, int y) {
return x+y;
}
SecondClass.h
class SecondClass: public Base
{
public:
virtual int funky(int x, int y);
};
SecondClass.cpp
int funky(int x, int y) {
return x+y;
}
In this condition I take error of "multiple definition." But, I have to use these functions with the same names.
I tried
int funky (int x, int y) override;
but it did not work.
I need all three function, because when I want to call them like below and if they are defined like above, I cannot reach them.
vector<Base> BASE = {FirstClass(), SecondClass()};
BASE[1]->funky(1,2)
Do you have any suggestion? I am open to another approach to this problem.
Thank you.
In the above code sample, you defining funky in two different cpp files but have declared in your class. So following should be the correct format:
FirstClass.cpp
int FirstClass::funky(int x, int y) {
return x+y;
}
SecondClass.cpp
int SecondClass::funky(int x, int y) {
return x+y;
}
main.cpp
FirstClass a;
SecondClass b;
cout<<a.funky(1,2)<<" "<<b.funky(3,4)<<endl;
int funky(int x, int y) in both your .cpp files are defined as free functions that has nothing to do with the classes you've defined. They have the same signature and you therefore get a linking problem with multiple definitions.
I suggest you make the destructor in the base class virtual, just in case you want to destroy objects through a base class pointer.
Inheriting, overriding and defining the member functions could look like this:
class Base {
public:
virtual ~Base() = default; // virtual destructor
virtual int funky(int x, int y) = 0; // = 0 added to make it pure virtual
};
class FirstClass : public Base { // inherit
public:
int funky(int x, int y) override; // now you can override
};
int FirstClass::funky(int x, int y) { // class member function definition
return x+y;
}
And you need to do the same for SecondClass.
You should not use vector<Base> though. You will not store Base objects but most probably pointers to objects of types derived from Base, like FirstClass and SecondClass. I suggest using the smart pointer std::unique_ptr for this:
#include <memory>
int main() {
std::vector<std::unique_ptr<Base>> BASE;
BASE.emplace_back(std::make_unique<FirstClass>());
BASE.emplace_back(std::make_unique<SecondClass>());
std::cout << BASE[0]->funky(2,3) << '\n';
std::cout << BASE[1]->funky(2,3) << '\n';
}
Demo
I'm writing a class which is wrapped in a namespace named cs. I am using a library whose one of the function takes a function pointer. The function have to modify some of the protected members of the class so I wrote a free function in the same cs namespace and made it a friend function of the class. Doing that made it available for the clients to use that function. But The function MUST be inaccessible from the client due to obvious reasons.
An example code is here:
#include "lib.h"
namespace cs{
class A
{
protected:
int x;
float y;
friend int myFunc(void* userdata, int valInt, float valFloat);
public:
void abc()
{
libFunc(this, myFunc);
}
};
void myFunc(void *userdata, int x, float y){
// I would like this function to be inaccessible from the client
A *obj = (A*) userdata;
obj->x = x;
obj->y = y;
}
}
If you want to make a free function inaccessible in another compilation unit, you may use a nested anonymous namespace:
namespace cs{
class A
{
protected:
//...
friend int myFunc(int valInt, float valFloat);
public:
void abc();
};
namespace { // anonymous nested namespace
int myFunc(int x, float y){
...
}
}
void A:: abc() {
libFunc(this, myFunc);
}
}
I am super-new to classes and still wrapping my brain around how they work. Any help/advice/pointers-> are appreciated!
I have two classes. Within the second class is an array of the first class. I am trying to assign values to the private member variables contained in the array of the first class.
I get this error message when compiling:
hw2Test.cpp: In member function 'void bar::set(int)':
hw2Test.cpp:11:7: error: 'int foo::x' is private
int x;
^
hw2Test.cpp:34:12: error: within this context
foodoo[0].x = x;
^
Here is the code:
#include <iostream>
using namespace std;
class foo
{
public:
private:
int x;
};
class bar
{
public:
void set(int x);
private:
foo foodoo[1];
};
int main()
{
bar tar;
tar.set(1);
return 0;
}
void bar::set(int x)
{
foodoo[0].x = x;
}
foo::x is declared as private, so only methods of foo can access it. But you are trying to access x inside of a method of bar instead, which does not have access to foo's private members.
To give bar access, you need to either:
declare foo::x as public:
class foo
{
public:
int x;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
declare a public setter:
class foo
{
public:
void set(int i);
private:
int x;
};
void foo::set(int i)
{
foodoo[0].x = i;
}
void bar::set(int x)
{
foodoo[0].set(x);
}
declare bar as a friend of foo:
class foo
{
public:
private:
int x;
friend class bar;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
I want to call a member function of another class on an object, but I cant seem to figure out how this works. As example code on how it should work:
Class A {
void somefunction(int x);
}
Class B : A {
void someotherfunction(int x);
}
Class C {
void x() {
callY(&ofthefunction);
} //here you call the function, you dont have an object yet, and you don't know the argument yet, this will be found in function callY
void Y(*thefunction) {
find int x;
if(something)
A a = find a;
a->thefunction(x);
else
B b = find b;
b->thefunction(x);
}
}
I hope this makes sence, It is also possible to split this in 2 methods, Y1 and Y2, but seeing as 90% of the code is the same (finding things in a XML file), only the object and argument where to save it is different, i'd like to do this
You can use something known as a virtual function. By the way, your syntax is hideous, it's class not Class, you need braces for your conditionals, and a judicious application of public, some extra semicolons, etc. It would be appreciated if you would go near a compiler before coming here, y'know.
class A {
public:
virtual void somefunction(int x);
};
class B : public A {
public:
virtual void somefunction(int x);
};
void func(A& a) {
int x = 0;
// Do something to find x
a.somefunction(x);
// calls A::somefunction if this refers to an A
// or B::somefunction if it's a B
}
int main() {
A a;
func(a); // calls A::somefunction
B b;
func(b); // calls B::somefunction
}
What you want to do can be done, although I woudn't solve it this way:
class A {
public:
virtual int doit(int x) { return x+1; }
};
class B : public A {
public:
int doit2(int x) { return x*3; }
int doit(int x) { return x*2; }
};
int foo(int (A::*func)(int), int x, bool usea) {
if (usea) {
A a;
return (a.*func)(x);
} else {
B b;
return (b.*func)(x);
}
}
int main() {
int (A::*bla)(int) = &A::doit;
foo(bla, 3, true);
foo(bla, 3, false);
}
However, for this to work, the following has to be satisfied:
You must use function pointers of the base class (e.g. int (A::*bla)(int)), otherwise you won't be able to call it on that base class (e.g. int (B::*bla)(int) can only be used on B instances, not on A instances, even if the method is already defined in A).
The methods must have the same names as in the base class
To use overriding (e.g. different impl in derived class), you have to use virtual functions.
But I would rather rethink your design...
No, that won't work at all. A pointer to a member of A will always point to that function, even when it's called on B because B inherits from A.
You need to use virtual functions. I see DeadMG has beaten me to it.
I'm having some problems with my class because they both depends on each other, to one can't be declared without the other one being declared.
class block: GtkEventBox {
public:
block(board board,guint x,guint y): image("block.png") {
this.board = board;
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
void move(guint x,guint y) {
board.remove(this);
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
private:
guint x, y;
board board;
GtkImage image;
};
class board: Gtk::Table {
public:
board(): Gtk::Table(25,20) {
blocks_c = 0;
}
void addBlock(guint x,guint y) {
blocks_a[blocks_c++] = new block(this,x,y);
}
private:
block* blocks_a[24];
int blocks_c;
};
As you can see the "block" class needs to know what a "board" is and vice versa. Thanks in advance!
Define "board" before "block" and forward declare the "block" class. Also, move the implementation of the board functions out of the class definition.
// forward declare block class
class block;
// declare board class
class board: Gtk::Table {
public:
board();
void addBlock(guint x,guint y);
private:
block* blocks_a[24];
int blocks_c;
};
// declare block class
class block: GtkEventBox {
public:
block(board board,guint x,guint y);
void move(guint x,guint y);
private:
guint x, y;
board board;
GtkImage image;
};
// define member functions (implementation) here...
Forward-declare your block class before board with this line:
class block;
Place the code of the function bodies AFTER declarations of both classes. Forward-declaring your class doesn't make all its functions available, it just allows the compiler to know that such class exists. It just allows to use, for instance, pointers to such a class (because the size of pointer type doesn't depend on the layout of the class).
This can easily be resoved with a forward declaration. Since board doesn't need to know anything much about the block class, only that it uses a pointer to it, declare it first. but before that include a forward declaration for block. It looks like this:
class block; // <- Forward declaration!
class board: Gtk::Table {
public:
board(): Gtk::Table(25,20) {
blocks_c = 0;
}
void addBlock(guint x,guint y) {
blocks_a[blocks_c++] = new block(this,x,y);
}
private:
block* blocks_a[24];
int blocks_c;
}; class block: GtkEventBox {
public:
block(board board,guint x,guint y): image("block.png") {
this.board = board;
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
void move(guint x,guint y) {
board.remove(this);
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
private:
guint x, y;
board board;
GtkImage image;
};
This is usually a design problem. I suggest you to pick the smaller class (in your case I'd suggest the block class) and code some events on it that the board class would sign. Then instead of calling the board class method, shoot the event and let the board class call it itself.