How would I fix this compiler error

Im declaring a "College" object like this
 
College DVC("Diablo Valley College");


And its giving me a compiler error, How would I fix this?

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
#include <iostream>
using namespace std;


class College {
public:
    College(string collegeName, int initialNum)
        : name(collegeName), numStudents(initialNum) {

        if (initialNum < 0) {
            numStudents = 0;
        }
    }

    void setName(string newName) {
        name = newName;
    }

    void setNumStudents(int newNum) {
        numStudents = newNum;
    }

    string getName() const {
        return name;
    }
    int getNumStudents() const {
        return numStudents;
    }

private:
    string name;
    int numStudents;
};
a compiler error

Would you show the error message so that we could help you read it?


(We know why it says something, but different compilers have different messages.)
Last edited on
its saying "no matching function call",

but when I tried someting like College::College DVC(DiabloValleyCollege);

it says college dvc is undefined
possibly: your ctor has 2 things, you provided 1.
dvc and DVC are not the same. College and college are not the same. Details matter here, don't approximate the code and the message, give us the real code and the real messages, preferably enough of the code to try to run it as a little stub program.
A different compiler says:
main.cpp:40:13: error: no matching constructor for initialization of 'College'
    College DVC("Diablo Valley College");
            ^   ~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:8:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char[22]' to 'const College' for 1st argument
class College {
      ^
main.cpp:8:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const char[22]' to 'College' for 1st argument
class College {
      ^
main.cpp:10:5: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
    College(string collegeName, int initialNum)
    ^
1 error generated.

The compiler generates some implicit member functions. In this case the compiler has these:
1
2
3
4
5
6
7
class College {
public:
    College(string, int);
    College(const College&);
    College(College&&);
   // other stuff
};

You wrote College DVC("Diablo Valley College");
It has one argument: "Diablo Valley College", which has type const char*
Does the list above have College(const char*);? No.

There are no instructions to create College from const char*.
You can't call College(string, int); with one argument, because it requires two arguments.

Two options:
* Add a constructor that does take one argument (for example College(string);)
or
* Add second parameter to the call. E.g. College DVC("Diablo Valley College", 9182645);
Yep, that was it, I didnt put in an int for the number of students, part of me thought it would just be initialized to 0 but whatever, thanks anyways.
You could declare the function: College(string collegeName, int initialNum = 0 )

Then you can write:
1
2
College DVC("Diablo", 42);
College lajolla("La Jolla"); // calls College("La Jolla", 0) 



Was "no matching function call" all that the compiler said?
No list of things it can't do (like the other compiler did show)?
Registered users can post here. Sign in or register to post.