code problems

OOook, lets try this... I'm trying to figure out how to acces class object from a class wihtout trying to acces object of an class from a start.

BTW: good thing is that i've started to comit my time to learing pointers.

anyway... i have 2 cpp files and one h files.

one cpp file is

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "opf.h"

using namespace std;

int main(){
	OPF *opf; // accessing the value of memory opf 
	
	string danilo="danilo";
	
	opf->name(danilo);
	
return 0;}


which is tester file, the file that i actualy want to compile

second file is .h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef OPF_H //operative functions
#define OPF_H // operative functions

#include <iostream>

using namespace std;

class OPF { //operative functions

public:
	//konstruktor i dekonstruktor
	OPF(string &somestring); // activating some string
	~OPF(); // deactivating some string
	
	
	string name(string &somestring);
	
private:


	
	string str; //deklaracija varijabla
protected:


};

#endif //OPF_H 


which is a header file that is included in a tester_file.cpp

and at least the other .cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "opf.h"

#include <string>
#include <iostream>

using namespace std;

OPF::OPF(string &somestring)
:str(somestring)
{
	OPF o;
	OPF *opf = &o;
}

string OPF::name(string &somestring)
{
		cout<<1<<endl;
	str=somestring;
	str.resize ( str.size() -4 ); cout<<1<<endl;
	str.append ( 1 , ' '); cout<<1<<endl;
	return str; cout<<1<<endl;
	str.clear(); cout<<1<<endl;
}

OPF::~OPF()
{
	OPF o;
	OPF *opf = &o;
}


which is .cpp file that is part of an opf.h file which is the that cointains all the formulas.

Atleast that is idea.

Im working on Windows, also i don't use IDE, i use notepad++ and GNU with TDC

sooo what is the point :

im trying to use constructor to call the object of its own class, and deconstructor to destory it. [ trying to stop over creating objects of the same class ] and it works.

also im trying [ but don't know if its possible ] to make all of functions from opf.h into one single opf.cpp file. and i'm worring about over creating constructor's objects.

so when i start the program but with single void print_f function it works. But when i start to compile the program above i get multiple errors.

compile cmd code is : g++ -o tester tester_file.cpp opf.h opf.cpp

and the ERROR is :

opf.cpp : in constructor 'OPF:OPF(std:string&)':
opf.cpp:11:6: error: no matching function for call to 'OPF:OPF()'
OPF o;

opf.cpp:11:6: note:candidates are:
in file included from opf.cpp:1:0:
opf.h:12:2:note:OPF::OPF(std::string&)
OPF(string &somestring);

opf.h:12:2: note: candidate expects 1 argument,0 provided
opf.h:8:7: note:OPF::OPF(const OPF&)
class OPF { // operative functions

opf.h:8:7: note: candidate expects 1 argument,0 provided
opf.cpp: indestructor 'OPF:~OPF()':
opf.cpp:27:6: error:no matching function for call to 'OPF::OPF()'
OPF o;

opf.cpp:27:6: note: candidates are
opf.cpp:8:1: note OPF::OPF(std::string &)
OPF::OPF(string &somestring)

opf.cpp:8:1: note: candidate expects 1 argument,0 provided
inf file include from opf.cpp:1:0:
opf.h:8:7: note OPF::OPF(const OPF&)
class OPF { //oper. functions

opf.h:8:7: note: candidate expects 1 argument,0 provided

i was looking at the code for about 2 hours, and can't find solution... any suggestions?

also , as you can see in string OPF::name (string &somestring) there are cout<<1<<endl; several times, i did this because when i loose all the variables in constructor, program alone terminates without error, so i tried looking where is going wrong. So i put 1ones to simulate the steps it does. and i will say that it only loads the first 1.
Last edited on
OPF o;

You haven't written a default constructor for your class. Seeing as you've written one that takes a string reference the compiler won't supply a default one.

1
2
3
4
5
OPF *opf; // accessing the value of memory opf 
	
	string danilo="danilo";
	
	opf->name(danilo);


That's very bad. You have not initialised your pointer.

And this in your ctor and dtor:
1
2
        OPF o;
	OPF *opf = &o;

is nonsense. It won't do anything. You create to local variables that will die when they go out of scope.

What exactly are you trying to achieve?

Also you might want to move this to Beginners forum. You'll get more people offering help that way.
Last edited on
i tried several topics in Beginners forum... no rply's.

I'm trying several things.
Im trying to learn headers and classes on my own... and many tutorials tell's you how to use void functions but not so much a functions with variables.

I tried to use .h file and in file to write functions... and that works, have some buggs, but works for some functions.

things im tring to do::

Initialize functions without calling the class for them, ( like example label->sometext )

trying to make a program that will automaticly call the compiler without me needing to call the g++ -o comand, by accessing strings as const chars in system.

trying to move from basic programing in one file and make my headers that will help me in my job. so i can call them when i need them.

Had some problems in undestanding pointers and refferences and trying to figure that out.

and trying to learn do i need a several .cpp files in one header or can i use just one.

and trying to minimize my compile time and size of my program by using fewer variables and trying to use their memory and reference without making copy of them and deleting that copy later.

and trying to figure out what is the sintaxe diference between making ordinary functions in class in main.cpp file and ordinary functions in some_header.cpp file which is in some_header.h file.



hopes this clarifies that :D
Last edited on
Initialize functions without calling the class for them, ( like example label->sometext )
I really don't understand this sorry.


trying to make a program that will automaticly call the compiler without me needing to call the g++ -o comand, by accessing strings as const chars in system.

I would really really recommend you use an IDE (like visual studio for example), then you can build and run your code without having to call a compile command from a console prompt.
Topic archived. No new replies allowed.