Issue
Problem
I'm trying to compile my main.cpp
file to main.exe
using the command:
g++ main.cpp -o main.exe
but I'm encountering an error.
Here's part of my code:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
long double temperature;
long double weight;
long double length;
long double time;
string unit_select;
string convert;
void newline()
{
cout << "\n" << endl;
}
void input_unit_convert()
{
cout << "Enter Unit" << ":" << " ";
cin >> convert;
std::transform(convert.begin(), convert.end(), convert.begin(), ::toupper);
}
void input_temperature()
{
cout << "Enter temperature" << ":" << " ";
cin >> temperature;
}
void input_weight()
{
cout << "Enter weight" << ":" << " ";
cin >> weight;
}
void input_length()
{
cout << "Enter length" << ":" << " ";
cin >> length;
}
void input_time()
{
cout << "Enter time" << ":" << " ";
cin >> time;
}
// some code here
do {
cout << "\n" << endl;
cout << "(T)" << " " << "Temperature" << "\n" << endl;
cout << "(W)" << " " << "Weight" << "\n" << endl;
cout << "(L)" << " " << "Length" << "\n" << endl;
cout << "(TI)" << " " << "Time" << "\n" << endl;
cout << "Select unit" << ":" << " ";
cin >> unit_select;
std::transform(unit_select.begin(), unit_select.end(), unit_select.begin(), ::toupper);
if (unit_select == "T" || unit_select == "W" || unit_select == "L")
{
// some code here
}
else if (unit_select == "TI")
{
for (auto& time_list : time_units)
{
cout << "\n" << time_list << "\n" << endl;
}
input_unit_convert();
// some code here
}
else if (convert == "MIL")
{
input_time();
newline();
cout << "Second" << ":" << " " << time * 31536000000 << endl;
cout << "Milisecond" << ":" << " " << time * 31536000000000 << endl;
cout << "Microsecond" << ":" << " " << time * 31536000000000000 << endl;
cout << "Nanosecond" << ":" << " " << time * 31536000000000000000 << endl;
cout << "Picosecond" << ":" << " " << time * 31536000000000000000000 << endl;
cout << "Minute" << ":" << " " << time * 525600000 << endl;
cout << "Hour" << ":" << " " << time * 8760000 << endl;
cout << "Day" << ":" << " " << time * 365000 << endl;
cout << "Week" << ":" << " " << time * 52142.8571 << endl;
cout << "Month" << ":" << " " << time * 12000 << endl;
cout << "Year" << ":" << " " << time * 1000 << endl;
cout << "Decade" << ":" << " " << time * 100 << endl;
cout << "Century" << ":" << " " << time * 10 << endl;
}
And I get this error:
main.cpp:1265:61: error: invalid operands of types 'time_t(time_t*)' {aka 'long long int(long long int*)'} and 'int' to binary 'operator*'
1265 | cout << "Century" << ":" << " " << time * 10 << endl;
| ~~~~ ^ ~~
| | |
| | int
| time_t(time_t*) {aka long long int(long long int*)}
Expectations
What did I tried
I changed the string from double
to long double
and still got the same error.
Expectation
I expect a main.exe
file in the same directory as the main.cpp
file.
Solution
As a couple of other users have pointed out, time
is a function name and you can't multiply by functions. That's what types 'time_t(time_t*)' {aka 'long long int(long long int*)'} and 'int' to binary 'operator*'
means. Renaming the variable to g_time
, gtime
or dTime
will fix the compilation error.
The use of "magic numbers" (around line 50) is also unnecessary and will lead to compiler warnings. Declare some constants to explain the purpose of this multiplication, like:
static const double Years_in_Millennium = 1000.0;
static const double Months_in_Millennium = Years_in_Millennium * 12.0;
static const double Days_in_Millennium = Years_in_Millennium * 365.0;
static const double Hours_in_Millennium = Days_in_Millennium * 24.0;
static const double Seconds_in_Millennium = Hours_in_Millennium * 3600.0;
static const double Microseconds_in_Millennium = Seconds_in_Millennium * 1000000.0;
and use them between lines 49 and 60.
Answered By - SmellyCat Answer Checked By - Marie Seifert (WPSolving Admin)