how fix this error

i want send a class to a function
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class cars{
protected:
    string a_marke,a_modelis;
    int var_turis,maks_grietis;
public:
    cars(string am="",string amm="",int vt=0,int mg=0){a_marke=am;a_modelis=amm;var_turis=vt;maks_grietis=mg;}
};
class Bclass:public cars{
public:
template<typename a>
 a get_info(int sk=1){
     if(sk==1)
     {
         return a_marke;
     }
     if(sk==2)
     {
         return a_modelis;
     }
          if(sk==3)
     {
         return var_turis;
     }
          if(sk==4)
     {
         return maks_grietis;
     }
     return 0;}
 void set_mark(string a=""){a_marke=a;}
 void set_modelis(string a=""){a_modelis=a;}
 void set_ark(int s=0){var_turis=s;}
 void set_mks_g(int s=0){maks_grietis=s;}
};
template<class t>
class store{

vector<t>komandos;
public:
    store(){}
     store operator + (store const &tka) {
        this->komandos.push_back(tka);

    }
};

void startup(store&sml){
    cout << "loading" << endl;
     ifstream filein;
        string str;
        filein.open("db.txt");
        int temp=0,temp2=0;
        int a1,a2;
        string a3,a4;

        while(getline (filein, str))/// nuskaito faila
        {

                if(temp==0)
                {
                    a3=str;
                }

                if(temp==1)
                {
                    a4=str;
                }
                if(temp==2)
                {
                    a1=strtof((str).c_str(),0);
                }
                if(temp==3)
                {
                    a2=strtof((str).c_str(),0);
                }
                temp++;
                if(temp==4)
                {
                    Bclass load_t;

                    load_t.set_mark(a3);
                    load_t.set_modelis(a4);
                    load_t.set_ark(a1);
                    load_t.set_mks_g(a2);
                    sml+load_t;
                    temp2++;
                    temp=0;

                }



        }
        for(int y=0; y!=temp2; y++)
        {
            cout<<"+";
        }
        cout<<endl;
        cout<<"u=krauta: "<<temp2<<endl;
        cout<<endl;

        filein.close();
}

error
||=== Build: Debug in 2nd_tadas (compiler: GNU GCC Compiler) ===|
D:\c++woėr\2nd_tadas\main.cpp|55|error: variable or field 'startup' declared void|
D:\c++woėr\2nd_tadas\main.cpp|55|error: missing template arguments before '&' token|
D:\c++woėr\2nd_tadas\main.cpp|55|error: 'sml' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
line 13:
void startup(store&sml)
Here the variable sml is missing template arguments,

for example use:
void startup(store<int>&sml)
if you want to store integers into comandos vector
malibor (183)
line 13:
void startup(store&sml)
Here the variable sml is missing template arguments,

for example use:
void startup(store<int>&sml)
if you want to store integers into comandos vector

thanks
but i want store two int and two strings
Then use vector of tuples like this:
std::vector<std::tuple<int, int, std::string, std::sting>>

that is if you want to have these 4 values associated, one with another.

othrewise, use 2 vectors, one for ints and one for strings:
1
2
std::vector<int>
std::vector<string>


but you lose association.

if you want more association (depends on your goal) use pairs:
std::vector<std::tuple<std::pair<int, string>, std::pair<int, string>>>
Last edited on
i need store in class and then i store class to vector ,but how to send class to function
Your store class adds no value. In fact, it makes things confusing. The statement sml+load_t; looks like an error. Just use vector<Bclass> instead and replace the odd add with sml.push_back(load_t);

Also, what's the purpose of Bclass? All it does it make the members of class cars accessible. I'll assume that you have some other reason for this, but if not, get rid of Bclass and put those methods directly in class cars.

Bclass::get_info() won't work. Consider this code:
1
2
3
4
int i;
Bclass bc;
cin >> i;
cout << bc.get_info(i);   // what is the type of bc.get_info()? 

The compiler needs to know the type of every expression at compile time. In this code, it won't know until run time.

To fix this, replace get_info() with get_marke(), get_modelis(), get_var_turis() and get_maks_grietis() methods.

To read Bclass, create a read() method (or >> operator if you know how to do that). This separates the reading part from the "adding to a vector" part, which may be useful. In fact, why not put that method in class cars instead?

Putting it all together:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ifstream;
using std::string;
using std::vector;

class cars
{
  protected:
    string a_marke, a_modelis;
    int	var_turis, maks_grietis;
  public:
    cars(string am = "", string amm = "", int vt = 0, int mg = 0) {
	a_marke = am;
	a_modelis = amm;
	var_turis = vt;
	maks_grietis = mg;
    }

    istream &
    read(istream &);
};

class Bclass: public cars
{
  public:
    string get_mark()
    {
	return a_marke;
    }
    string get_modelis()
    {
	return a_modelis;
    }
    int get_ark()
    {
	return var_turis;
    }
    int get_mks_g()
    {
	return maks_grietis;
    }

    void set_mark(string a = "") {
	a_marke = a;
    }
    void set_modelis(string a = "") {
	a_modelis = a;
    }
    void set_ark(int s = 0) {
	var_turis = s;
    }
    void set_mks_g(int s = 0) {
	maks_grietis = s;
    }
};

istream & cars::read(istream & strm)
{
    getline(strm, a_marke);
    getline(strm, a_modelis);
    strm >> var_turis >> maks_grietis;
    return strm;
}

void
startup(vector < Bclass > &sml)
{
    cout << "loading" << endl;
    ifstream filein;
    filein.open("db.txt");
    Bclass load_t;

    sml.clear();				 // you'd be amazed how often people for to do this
    while (load_t.read(filein)) {
	sml.push_back(load_t);
    }
    for (unsigned y = 0; y != sml.size(); y++) {
	cout << "+";
    }
    cout << endl;
    cout << "u=krauta: " << sml.size() << endl;
    cout << endl;

    filein.close();
}




thanks for help
Topic archived. No new replies allowed.