Error C2660

Pages: 12
Can you just get rid of the declaration of the set function? It is not needed if you have an initialiser list in the constructor.

With the initialiser list, you should realise that the purpose of the constructor is to initialise variables, so that is why the initialiser list must be part of a constructor. Have a read of this:

http://www.cprogramming.com/tutorial/initialization-lists-c++.html


If there are still problems, then post your code again, plus any errors you have.
I am reposting my code because I am still having issues:

.h 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
30
31
32
33
34
35
36
#pragma once
#include <string>
#include <iostream>
using namespace std;

class Flight
{
private: 
	int FlightNumber;
	string DestinationCity;
	string DepartureTime;
	string DepartureGate;
	int DepartureMonth;
	int DepartureDate;

	
public:
	Flight(void);
	Flight(int setFlightNumber, string setDestinationCity, string setDepartureTime, string setDepartureGate, int setDepartureMonth, int setDepartureDate):
	FlightNumber(setFlightNumber),
	DestinationCity(setDestinationCity),
	DepartureTime(setDepartureTime),
	DepartureGate(setDepartureGate),
	DepartureMonth(setDepartureMonth),
	DepartureDate(setDepartureDate)
	{}	
	~Flight(void);
	
	int set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate);

	int getMonth();
	int getDate();

	void show();

};


.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
#include <iostream>
#include <string>
#include "Flight.h"
using namespace std;

Flight::Flight(void)
{
}

Flight::~Flight(void)
{
}

int Flight::getMonth() {
	return DepartureMonth;
}

int Flight::getDate() {
	return DepartureDate;
}

void Flight::show() {
	cout << "Flight " << FlightNumber << " for " << DestinationCity << " leaving at " << DepartureTime << " from gate " << DepartureGate << endl; 
}
   


Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <string>
#include "Flight.h"
using namespace std;

void runTravel() {
	Flight leg[3];
	// flight #, destination, departure time, gate, month, date
	leg[0].set(225, "Rome", "8:00 am", "B12", 4, 21);
	leg[1].set(16, "Vienna", "11:30 am", "A5", 4, 21);
	leg[2].set(27, "Moscow", "4:15 pm", "C9", 4, 21);
	cout << "Here is your itinerary beginning on "
		<< leg[0].getMonth() << "/" << leg[0].getDate() << ":\n";
	for (int i = 0; i < 3; i++)
		leg[i].show();
	//Flight 225 for Rome leaving at 8:00 am from gate B12.
}

int main() {
	runTravel();
	return 0;
}


These are the errors that I am getting:

1
2
3
4
5
6
7
8
1
1>  Sops_15.cpp
1>  Generating Code...
1>  Compiling...
1>  Flight.cpp
1>  Generating Code...
1>Sops_15.obj : error LNK2019: unresolved external symbol "public: int __thiscall Flight::set(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int)" (?set@Flight@@QAEHHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00HH@Z) referenced in function "void __cdecl runTravel(void)" (?runTravel@@YAXXZ)
1>C:\Users\David Sopshin\Documents\Visual Studio 2010\Projects\Sops_15\Debug\Sops_15.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In your .h file at line 25, you have a declaration for your set function.
Where did you implement the set function?
It's not in your .cpp file.
I guess I need to implement it in the .cpp file? I will try that and see what happens.
So this is my new .cpp file with (what I hope to be) the implementation of the set 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
#include <iostream>
#include <string>
#include "Flight.h"
using namespace std;

Flight::Flight(void)
{
}

Flight::~Flight(void)
{
}

int set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate);

int Flight::getMonth() {
	return DepartureMonth;
}

int Flight::getDate() {
	return DepartureDate;
}

void Flight::show() {
	cout << "Flight " << FlightNumber << " for " << DestinationCity << " leaving at " << DepartureTime << " from gate " << DepartureGate << endl; 
}
   


I am still getting the same error as above however.
That is why I mentioned the use of a vector previously. That way you can make use of the initialiser list in the constructor - that was my whole point of mentioning initialiser lists to get rid of the need for set functions. To do this you have to put your objects into a container in such a way that your particular constructor (with it's initialiser list) is called.

By using an array, all it's elements are created using a default constructor (the one with no parameters), which means you have to write your own set function to update with the values you want, because the values have already been set to some default value.

You could have an array of pointers or references to your objects, but this is a little trickier.

By using a vector, you can avoid all this. I am hoping you are allowed to use vectors - IMO if you are allowed to do classes, the you should be allowed to do vectors also.

In C++11, there is this function, which calls the appropriate constructor based on the args given to it, without any copying or moving:

http://www.cplusplus.com/reference/vector/vector/emplace_back/


A better example here:

http://en.cppreference.com/w/cpp/container/vector/emplace_back


I noticed you have some of the types as int again - so it is acceptable to have negative values for these things (gate numbers, dates etc)
I am still getting the same error as above however.

Line 14 of your .cpp file is a declaration, not an implementation.
Nor is it a member of your class.

1
2
3
int Flight::set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate)
{  // do something here
}





Last edited on
Line 14 of your .cpp file is a declaration, not an implementation.
Nor is it a member of your class.


So I tried to do something as you suggested above, but now I get errors that "Flight::set may not be redeclared outside its class."

I don't really understand what this means.
Without seeing the code, how can we know what's wrong?
That's a good point. Here is my current code:

.h 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
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <string>
#include <iostream>
using namespace std;

class Flight
{
private: 
	int FlightNumber;
	string DestinationCity;
	string DepartureTime;
	string DepartureGate;
	int DepartureMonth;
	int DepartureDate;
	int set;
	
public:
	Flight(void);
	Flight(int setFlightNumber, string setDestinationCity, string setDepartureTime, string setDepartureGate, int setDepartureMonth, int setDepartureDate):
	FlightNumber(setFlightNumber),
	DestinationCity(setDestinationCity),
	DepartureTime(setDepartureTime),
	DepartureGate(setDepartureGate),
	DepartureMonth(setDepartureMonth),
	DepartureDate(setDepartureDate)
	{}	
	~Flight(void);

	int set;
	
	Flight set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate);

	int getMonth();
	int getDate();

	void show();

};


.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
#include <iostream>
#include <string>
#include "Flight.h"
using namespace std;

Flight::Flight(void)
{
}

Flight::~Flight(void)
{
}

int Flight::set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate);

int Flight::getMonth() {
	return DepartureMonth;
}

int Flight::getDate() {
	return DepartureDate;
}

void Flight::show() {
	cout << "Flight " << FlightNumber << " for " << DestinationCity << " leaving at " << DepartureTime << " from gate " << DepartureGate << endl; 
}
   


Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include "Flight.h"
using namespace std;

void runTravel() {
	Flight leg[3];
	// flight #, destination, departure time, gate, month, date
	leg[0].set(225, "Rome", "8:00 am", "B12", 4, 21);
	leg[1].set(16, "Vienna", "11:30 am", "A5", 4, 21);
	leg[2].set(27, "Moscow", "4:15 pm", "C9", 4, 21);
	cout << "Here is your itinerary beginning on "
		<< leg[0].getMonth() << "/" << leg[0].getDate() << ":\n";
	for (int i = 0; i < 3; i++)
		leg[i].show();
	//Flight 225 for Rome leaving at 8:00 am from gate B12.
}

int main() {
	runTravel();
	return 0;
}



Thank you.
Line 14 of your .cpp file has no body. Note the ; at the end of the line.

One other problem, you have the variable set declared at both lines 15 and 29 of your .h file.

Also, it is not best practice to have a method and a variable named the same. i.e. set is both a variable and a function in your class.
You still have not defined the method Flight.set(). All you've done is declared it twice - once inside the header, at line 31, and once in the cpp file, at line 14. It's the one in the cpp file that the compiler is complaining about - as you'd know, if you read the compiler warning more carefully, including the line number it reports.

You obviously know how to define a method, since you've successfully done it for all your other methods. Why are you not doing it for this one too?

Also, I've noticed you've declared both a private member variable, and a public one, called "set". What's that about?
Every time I try to define something, I get errors that it is not a part of the class. I'm not sure why that's not working though.
closed account (Dy7SLyTq)
you need to define set bro. else all of your members are unitialized
Every time I try to define something, I get errors that it is not a part of the class. I'm not sure why that's not working though.

Well, what are you doing differently from all the times you've already successfully done it for the other methods?
I think I'm trying to define it like I am defining Flight, but that's not working. Which is why I was trying to do int set().
1
2
3
4
5
6
7
8
int Flight::set(int FlightNumber, string DestinationCity, string DepartureTime, string DepartureGate, int DepartureMonth, int DepartureDate)
{  this->FlightNumber = FlightNumber;
    this->DestinationCity = DestinationCity; 
    this->DepartureTime = DepartureTime;
    this->DepartureGate = DepartureGate;
    this->DepartureMonth = DepartureMonth;
    this->DepartureDate = DepartureDate;
}


Note: It's not best practice to name your arguments the same as your member variables. As you can see from the above snippet, I had to qualify every member variable with this-> so the compiler could tell the argument name apart from the member name.

One common convention is to prefix your member variables with m_
i.e. m_FlightNumber



Note: It's not best practice to name your arguments the same as your member variables. As you can see from the above snippet, I had to qualify every member variable with this-> so the compiler could tell the argument name apart from the member name.

One common convention is to prefix your member variables with m_
i.e. m_FlightNumber


Thank you so much for your help. That finally makes sense, and it allowed the file to compile properly. Thanks for everyone's help!
> I had to qualify every member variable with this->
> so the compiler could tell the argument name apart from the member name.
¿and that's a problem because?

Also, good job making his homework. He only needed to wait four days.
Topic archived. No new replies allowed.
Pages: 12