Need a little help with my void function

I can't get my code to compile. Any help would be appreciated!

(the Error)
temperature.cpp: In function ‘void readTemp(std::vector<Temperature>)’:
temperature.cpp:66:16: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<Temperature>, Temperature>::value_type’ {aka ‘Temperature’} and ‘int’)
66 | temps[i]=j;
| ^
temperature.cpp:17:7: note: candidate: ‘constexpr Temperature& Temperature::operator=(const Temperature&)’
17 | class Temperature
| ^~~~~~~~~~~
temperature.cpp:17:7: note: no known conversion for argument 1 from ‘int’ to ‘const Temperature&’
temperature.cpp:17:7: note: candidate: ‘constexpr Temperature& Temperature::operator=(Temperature&&)’
temperature.cpp:17:7: note: no known conversion for argument 1 from ‘int’ to ‘Temperature&&’


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
/*
 File: temperature.cpp
  Created by: ??
  Creation Date: ??
  Synopsis: ??
*/

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

class Temperature
{
private:
	double degree;
	bool warm;
	double getKelvin() const;

public:
	double getDegree() const;
	void setDegree(const double temp);
	bool getWarmth() const;
	void setWarmth(const bool warmth);
	string toString() const;
};

// FUNCTION PROTOTYPES GO HERE:
string getFileName ();
void readTemp (vector<Temperature> temps);
int main()
{	
	vector<Temperature> temps;
	string file_name;
	
	Temperature temp;
  temp.setDegree(75.0);
  cout << temp.toString() << endl;
   // 1. Call the function described in TASK 4 here
   getFileName ();
   // 2. Call the function described in TASK 5 here
   readTemp (temps);
	cout << "The vector size is " << temps.size() << endl << endl;
	// 3. Call the function described in TASK 6 here
	// 4. Call the function described in TASK 7 here


	
	return 0;
}
		 
// FUNCTION DEFINITIONS GO HERE:
void readTemp (vector<Temperature> temps) {
   int tempNum;
   cout << "Number of temperatures you will enter: " << endl;
   cin >> tempNum;
   cout << "Enter temperatures (Fahrenheit): "<< endl;
   int j=0;
   for (int i=0; i < tempNum; i++) {
      cin >> j;
      if ((j >=30) &&(j <=100)) {
      temps[i]=j;
      }
   }
}

      
double getKelvin() {
   double k;
   Temperature temp;
   k=((temp.getDegree() - 32) * 5 / 9 + 273.15);
  std::to_string(k);
  return k;
   
}
string getFileName () {
   cout << "Enter file name: " << endl;
   string file_name;
   cin >> file_name;
   return file_name;
}
 
// CLASS MEMBER FUNCTION DEFINITIONS GO HERE:
double Temperature::getDegree() const{
   return degree;
}
bool Temperature::getWarmth() const{
   return warm;
}
void Temperature::setDegree(const double temp){
   degree=temp;
}
void Temperature::setWarmth(const bool warmth) {
   warm=warmth;
}
string Temperature::toString() const {
   string str;
   string k;
  	double getKelvin();
   str= to_string(getDegree()) + " F ("  + std::to_string(getKelvin()) + " K)";
  return str;
}
   
Well, line 66 says “assign an int to a variable of type Temperature”.
But you haven’t defined such an assignment.

Maybe you could use your member function setDegree() instead.

While you are at it, please obliterate lines 76 and 103 as well.

And in function readTemp() pass the argument by reference, or you’ll never see it again.
Last edited on
I know the code seems redundant, it is. I'm just following steps for this assignment and I'm on this one:
Write the prototype and definition for a function that prompts and reads temperatures (in Fahrenheit) from the user and stores the valid temperatures into a list of objects defined from the class Temperature. The function has one input parameter that is a vector holding objects defined from the class Temperature. The function does not return a value.

The function first prompts the user for the number of temperatures with the prompt "Number of temperatures you will enter: ". Next, prompt the user to enter the specified number of temperatures with the prompt "Enter temperatures (Fahrenheit): ". Only store valid temperatures (see below) into the vector given in the input parameter. Warm temperatures (see below) will have the member variable warm (see the class Temperature) set to true. Other valid temperatures will have the member variable warm set to false. Use the member function setWarmth here. Display to the screen the number of valid temperatures that were stored in the list, i.e. vector. For example if 10 temperatures are entered and 6 of those were valid temperatures then output "Kept 6 out of 10 temperature(s).".
Hello olivergwin,

In "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
int main()
{
    string file_name;
    vector<Temperature> temps;

    Temperature temp;

    temp.setDegree(75.0);

    cout << temp.toString() << endl;

    // 1. Call the function described in TASK 4 here
    getFileName();

    // 2. Call the function described in TASK 5 here
    readTemp(temps);

    cout << "The vector size is " << temps.size() << endl << endl;

    // 3. Call the function described in TASK 6 here
    // 4. Call the function described in TASK 7 here

    return 0;
}

I added some blank lines to make it easier to read.

Lines 3 and 4 I switched the order, does not make any difference just a personal thing.

Line 4 creates a vector of classes, but it is empty at this point.

Not sure what lines 6 and 7 are for.

Line 16 calls the function passing the vector of classes. still empty at this point.

In the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void readTemp(vector<Temperature>& temps)
{
    int tempNum;

    cout << "Number of temperatures you will enter: " << endl;
    cin >> tempNum;

    cout << "Enter temperatures (Fahrenheit): " << endl;

    int j = 0;

    for (int i = 0; i < tempNum; i++)
    {
        cin >> j;  // <--- The "cin" ALWAYS needs a prompt.

        if ((j >= 30) && (j <= 100))
        {
            //temps[i] = j;  // <--- Commented to get ti to compile.
        }
    }
}

First in line 1 pass the vector by reference. It is also a good idea to pass "std::string"s by reference.

The problem here is that the vector is still empty.

In line 18 you are trying to access the empty vector by using a subscript, except there is nothing to access. The second part of that is and as lastchance mentioned you would need to call a set function.

To start with you need to add a default ctor and an overloaded ctor to you class.

The "readTemp" function should collect any input into local variable(s) then in place of line put: temps.emplace_back(variable(s). This will call the overloaded ctor of the class to create an object of the class before it adds it to the vector.

The function should be adding to the vector not trying to access something that is not there.

Other than adding some blank lines to your code that is as far as I have worked with.

Still need to make the changes and test, but I believe the concept is sound.

Andy
k=((temp.getDegree() - 32) * 5 / 9 + 273.15);

There is integer division happening there. When dealing with doubles, I prefer to put digits before and after the decimal point, it saves the compiler from having to implicitly do a cast from int to double.

k=((temp.getDegree() - 32.0) * 5.0 / 9.0 + 273.15);

those magic numbers (32.0, 5.0 / 9.0, 273.15) could be stored as private constexpr double values in the class:

1
2
3
4
private:
   constexpr double FtoCConst = 32.0;
   constexpr double FtoCFactor = 5.0 / 9.0; // I prefer to get compiler to do division for irrationals
   constexpr double KelvinConst = 273.15;


Now the code becomes:

k=((temp.getDegree() - FtoCConst) * FtoCFactor + KelvinConst);

It is good practise to look at any expression that involves a division, to check that it is not zero, at least in the testing phase.

if you had :

1
2
3
constexpr double FtoCFactor = 5/ 9; // wrong

if (FtoCFactor < 1e-5) {std::cout << "FtoCFactor close to zero\n";}


Output:
FtoCFactor close to zero


The 1e-5 is an arbitrary precision value that I chose, you could have some other value and make it a constexpr variable as well.

This seems pretty trivial now that you have learnt about integer division, but one should always do these checks for more complex expressions that have division, either with code or some other method that proves the denominator cannot be zero. For example in some math formulae, if the input is in a valid range the denominator will never be zero.

Good Luck !!
Last edited on
Not withstanding TheIdeasMan valid comments re constants, as a starter that compiles and runs OK as far as the program goes, consider:

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 <iomanip>
#include <vector>

class Temperature
{
	double degree {};
	bool warm {};

public:
	Temperature() {}
	Temperature(double d) : degree(d) {}

	double getKelvin() const;
	double getDegree() const;
	void setDegree(double temp);
	bool getWarmth() const;
	void setWarmth(bool warmth);
	std::string toString() const;
};

// FUNCTION PROTOTYPES GO HERE:
std::string getFileName();
void readTemp(std::vector<Temperature>& temps);

int main()
{
	std::vector<Temperature> temps;
	std::string file_name;
	Temperature temp(75.0);

	std::cout << temp.toString() << '\n';

	// 1. Call the function described in TASK 4 here
	getFileName();

	// 2. Call the function described in TASK 5 here
	readTemp(temps);
	std::cout << "The vector size is " << temps.size() << "\n\n";

	// 3. Call the function described in TASK 6 here
	// 4. Call the function described in TASK 7 here
}

// FUNCTION DEFINITIONS GO HERE:
void readTemp(std::vector<Temperature>& temps) {
	size_t tempNum {};

	std::cout << "Number of temperatures you will enter: ";
	std::cin >> tempNum;

	std::cout << "Enter temperatures (Fahrenheit): ";

	double j {};

	for (size_t i = 0; i < tempNum && std::cin >> j; ++i)
		if ((j >= 30) && (j <= 100))
			temps.emplace_back(j);
}

std::string getFileName() {
	std::string file_name;

	std::cout << "Enter file name: ";
	std::cin >> file_name;

	return file_name;
}

// CLASS MEMBER FUNCTION DEFINITIONS GO HERE:
double Temperature::getKelvin() const {
	return ((getDegree() - 32.0) * 5.0 / 9.0 + 273.15);
}

double Temperature::getDegree() const {
	return degree;
}

bool Temperature::getWarmth() const {
	return warm;
}
void Temperature::setDegree(const double temp) {
	degree = temp;
}
void Temperature::setWarmth(const bool warmth) {
	warm = warmth;
}

std::string Temperature::toString() const {
	return std::to_string(getDegree()) + " F (" + std::to_string(getKelvin()) + " K)";
}

Last edited on
Topic archived. No new replies allowed.