Error with class array

Hi, I've started on a project and got this error in my main:

error: no matching function for call to 'Automobilis::Automobilis()' (line 12)

Any ideas/tips?



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
103
104
105
106
107
  #include <iostream>
#include "Automobilis.h"
#include <fstream>
#include <string>

using namespace std;

void Duomenys1(Automobilis A[],int &m);

int main()
{ int m;
Automobilis     A[m];

Duomenys1(A,m);
    return 0;
}
void Duomenys1(Automobilis A[],int &m)
{
    string e1,e2; int kl; double kr;
    ifstream fd("Studentas1.o");
    fd >> m;
    for (int i = 0; i<m; i++)
    {
       getline(fd,e1);
       fd >> e2 >> kl >> kr;
       A[i].DetiStudenta(e1);
       A[i].DetiMarke(e2);
       A[i].DetiKel(kl);
       A[i].DetiKur(kr);
    }
    fd.close();
}

#include "Automobilis.h"

Automobilis::Automobilis()
{ string Automobilis::ImtiStudenta()
{
    return vardas;
}
  void Automobilis::DetiStudenta(string vardas)
  {
      vardas = stud;
  }
  string Automobilis::ImtiMarke()
  {
      return marke;
  }
  void Automobilis::DetiMarke(string marke)
  {
      marke = mar;
  }
  int Automobilis::ImtiKel()
  {
      return keleiviai;
  }
  void Automobilis:: DetiKel(int keleiviai)
  {
      keleiviai = kel;
  }
  double Automobilis::ImtiKur( )
  {
      return kuras;
  }
  void Automobilis::DetiKur(double kuras)
  {
      kuras = kur;
  }
    //ctor
}

Automobilis::~Automobilis()
{
    //dtor
}

#ifndef AUTOMOBILIS_H
#define AUTOMOBILIS_H
#include <string>

using namespace std;

class Automobilis
{
    public:
        string ImtiStudenta();
        void DetiStudenta(string vardas);
        string ImtiMarke();
        void DetiMarke(string marke);
        int ImtiKel();
        void DetiKel(int keleiviai);
        double ImtiKur();
        void DetiKur(double kuras);
        Automobilis(string stud,string mar,int kel, double kur);
        virtual ~Automobilis();
    protected:

    private:
        string stud; //studento vardas
        string mar; //automobilio marke
        int kel; //keleiviu skaicius
        double kur; //sunaudojamo kuro kiekis

};

#endif // AUTOMOBILIS_H
Last edited on
Hello jusurb,

You have no constructor defined for the class. What you have is:

1
2
3
4
5
6
Automobilis::Automobilis()
{
	string Automobilis::ImtiStudenta()
	{
		return vardas;
	}


No closing brace "}" for "Automobilis::Automobilis".

I think what you meant was:

1
2
3
4
5
6
7
8
Automobilis::Automobilis()
{
}

string Automobilis::ImtiStudenta()
{
	return vardas;
}


Hope that helps,

Andy

P.S. I could be wrong, but I do not believe the "virtual" in virtual ~Automobilis(); is needed.

P.S.S. I found the closing brace. Line 70 needs to deleted.
Last edited on
Hello jusurb,

You should compile you program before posting it and try to fix everything you can before posting. Include any errors that you do not understand with your other questions.

When I tried to check your code I found that the class is missing four definitions that the "Automobilis.cpp" file is trying to access.

I figured out that you included all the member function definitions inside the constructor. Each definition of a member function needs to be separate and the default constructor just contains a set of empty braces as I showed you in my previous message.

Testing the program did not work because I do not know what is in the file "Studentas1.o". Include the file in a message if it is small or at least five entries for testing.

Without the file "Studentas1.o" the function void Duomenys1(Automobilis A[], int &m) did not work because the file did not open. You could add this code to check the file status:

1
2
3
4
5
6
7
8
9
10
11
	if (fd.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));
		exit(1);
	}


"iFileName" is defined as string iFileName{ "Studentas1.o" };. It was easier to add that one line than change everything in the stock code I use. I only changed the "fd" in the first line to match your program. The second line of the if/else requires the header files "chrono" and "thread" to work. You may find that useful in the future. The exit(1); of the "else" is because there is no need to go any farther with the program. The "1" tells whoever called the program that there was a problem. Not always needed, but useful.

In the function void Duomenys1(Automobilis A[], int &m) lines 21 - 24 will be a problem. Line 21 fd >> m; will input something into "m", but possibly leave a "\n" in the input buffer that the "getline" on line 24 will read from the buffer first and not the file.

1
2
fd >> m;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


A "cin >> something or inputFile >> something" followed by a "std::getline()" will need this to clear the input buffer before the "getline()". Line 2 requires the header file "limits" to work.

Hope that helps,

Andy
Topic archived. No new replies allowed.