Error expression must have integral or unscoped enum type

I get this error for infile>>num. Why is that so?
  • Remove From My Forums
  • Question

  • int loop;
    int num;
    FILE *infile = fopen(inputFile, "r");
      if (infile != NULL) {
        infile >> num;
        data[loop] = num;
        std::cout << data[loop] << std::endl;
      }

    I get this error for infile>>num. Why is that so?

Answers

  • On 12/27/2018 9:15 AM, Brown12345 wrote:

    [code]
    int loop;
    int num;
    FILE *infile = fopen(inputFile, «r»);
       if (infile != NULL) {
         infile >> num;

    This works for C++ istream and similar, not for C-style FILE* handle. Why don’t you use std::ifstream for your file reading needs?

    • Proposed as answer by

      Friday, December 28, 2018 3:30 AM

    • Marked as answer by
      Brown12345
      Friday, December 28, 2018 4:42 AM

So far I have the following code:

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
// Purpose: To write a program that displays the number of millimeters higher the current level the ocean level will be in
// in 5, 7, and 10 years.

# include <iostream>
# include <string>
using namespace std;

int main()
{
	float X = 10;

	string Y="";
	
	X = 5, 7, 10;

	cout << X << endl; 
	cout << "Enter the number of the desired yearn"
		<< "tells you the number of millemeters the current ocean level is.: ";
	getline(cin, Y);
	cin >> Y;

	cout << "The number of millimeters higher the current level of the ocean in 5 years is; " << Y = X * 1.5 << '.' << endl;
	
	cout << "The number of millimeters higher the current level of the ocean in 7 years is; " << Y = X * 1.5 << '.' << endl;
	
	cout << "The number of millimeters higher the current level of the ocean in 10 years is; " << Y = X * 1.5 << '.' << endl;
	cin >> Y;

	cout << "Press the Enter key to continue...";
	cin.get();
	return 0;
}	//end main() 

, but I get the following error message:

IntelliSense: expession must have integral or unscoped enum type

three times in a row for lines 25, 27, and 29 and I don’t understand or know why?

In case the purpose does make sense here are the directions:

2.7: Ocean Levels
Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a program that displays
• The number of millimeters higher than the current level that the ocean’s level will be in 5 years,
• The number of millimeters higher than the current level that the ocean’s level will be in 7 years,
• The number of millimeters higher than the current level that the ocean’s level will be in 10 years,

Output labels: Each value should be on a line by itself, preceded by the a label of the form:
In X years the ocean’s level will be higher by Y millimeters.
where X is the number of years (5, 7 or 10) and Y is the value you calculate.

Does any else have an idea?

Last edited on

This is probably not doing what you’re expecting it too. X will only hold one value, not three.

X = 5, 7, 10;

You’re trying to do an assignment to Y within the three output statements. I’m guessing you didn’t mean to try to overwrite the year which the user entered in Y. What are you trying to output here?

cout << "The number of millimeters higher the current level of the ocean in 5 years is; " << Y = X * 1.5 << '.' << endl;

If you edit your post, highlight the code part, then click on the <> button in the Format palette at the right of the post, your code will format for the forum with line numbers etc.

Last edited on

What your saying is that I can’t assign X three values, but I can assign X to multiple assignments?

Also where am I trying to do an assignment to Y within the three output statements and overwrite the year, which the user entered in Y?

Output labels: Each value should be on a line by itself, preceded by the a label of the form:
In X years the ocean’s level will be higher by Y millimeters.
where X is the number of years (5, 7 or 10) and Y is the value you calculate.

Quit thinking of X and Y as variables. They are simply place holders in the text of the problem description to indicate where certain values should be displayed in the output. They don’t necessarily have any place in the code as variables.

You have defined Y as a string and X as a float. The expression Y = X * 1.5 is nonsensical to the compiler. If you’re using C++11, you could use Y = std::to_string(X * 1.5) to get it to compile, however the assignment to string isn’t necessary because our std::ostream handles the display of floating point values just fine:
cout << "The number ... is: " << X*1.5 << ".n";

I know it says expression must have integral or unscoped enum type, but what does that mean? Also I tried a scoped enum as follows, but it didn’t like it either:

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
// Purpose: To write a program that displays the number of millimeters higher the current level the ocean level will be in
// in 5, 7, and 10 years.

# include <iostream>
using namespace std;

int main()
{
	int Y;
	int yearValue; // To hold year number desired.
	enum year { X=5, X2=7, X3=10};
	

	Y = (yearValue + X) * 1.5;
	Y = (yearValue + X2) * 1.5;
	Y = (yearValue + X3) * 1.5;

	cout << "Enter the current year";
	cin >> yearValue;
	cout << "The number of millimeters higher the current level of the ocean in 5 years is; " << Y = (yearValue + X) * 1.5 << '.' << endl;
	cout << "The number of millimeters higher the current level of the ocean in 7 years is; " << Y = (yearValue + X2) * 1.5 << '.' << endl;
	cout << "The number of millimeters higher the current level of the ocean in 10 years is; " << Y = (yearValue + X3) * 1.5 << '.' << end;

	cout << "Press the Enter key to continue...";
	cin.get ();
	return 0;
}	//end main() 

Last edited on

You’re overwriting the value of Y here each time you make a new assignment. And yearValue won’t have a value yet since the user doesn’t input the year until after this code.

1
2
3
Y = (yearValue + X) * 1.5;
Y = (yearValue + X2) * 1.5;
Y = (yearValue + X3) * 1.5;

The code still has this assignment statement in the output stream. (assignment meaning the code is doing a calculation and trying to assign it to Y.)

<< Y = (yearValue + X) * 1.5 <<

I might do something like this — I’ve just put one option here for 5 years, but it would be similar for the other years. Using a constant variable instead of hardcoding the specific number makes it easier later on to change the time periods without having to search through the code to update in multiple places. This is a short program, so it’s not a big deal here, but just something to keep in mind.

You might also want to add in some validation to make sure that they’ve input a valid year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# include <iostream>
using namespace std;

int main()
{
    const int periodA = 5;
    int year = 0;
    
    cout << "Enter the current year: ";
    cin >> year;
    cout << "In " << periodA << " years, the ocean will be " << (year + periodA) * 1.5 << " millimeters higher than its current level." << endl;

    cout << "Press the Enter key to continue...";
    cin.get ();
    return 0;
}

I solved a few problems but aquired some more unless they already existed. My current source code is as follows:

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
// Purpose: To write a program that displays the number of millimeters higher the current level the ocean level will be in
// in 5, 7, and 10 years.

# include <iostream>
# include <string>
using namespace std;

int main()
{
	string YearValue(); // fixes error with illegal operand on + symbol
	int leap(double X1, double X2, double X3, double Y1, double Y2, double Y3);
	int count(int YearVariable);
	double X1 = 5; // fixes error with X1 not defined
	double X2 = 7; // fixes error with X2 not defined
	double X3 = 10; // fixes error with X3 not defined
	double Y1 = (YearValue + X1) * 1.5; // fixes error with Y1 not defined
	double Y2 = (YearValue + X2) * 1.5; // fixes error with Y2 not defined
	double Y3 = (YearValue + X3) * 1.5; // fixes error with Y3 not defined
	
	enum year { X1 = 5, X2 = 7, X3 = 10 }; // fixes error with expression must have integral or unscoped enum type.
	enum millhigher {Y1 =  (YearValue + X1) * 1.5 const , Y2 = (YearValue + X2) * 1.5 const, Y3 = (YearValue + X3) * 1.5 const } ; // fixes
	//				error with expression must have integral or unscoped enum type.

	cout << "Enter the current year: ";
	cin >> YearValue;
	cout << "The number of millimeters higher the current level of the ocean in" << X1 << "years will be " << Y1 << '.' << endl; // prints output
	cout << "The number of millimeters higher the current level of the ocean in" << X2 << "years will be" << Y2 << '.' << endl; // prints output
	cout << "The number of millimeters higher the current level of the ocean in" << X3 << "years will be" << Y3 << '.' << endl; // prints output

	cout << "Press the Enter key to continue..."; // ends program
	cin.get (); //verbose or display output on screen by talking in text to user.
	return 0;
}	//end main() 

Any idea how to solve the following errors:

35 IntelliSense: no operator «>>» matches these operands
operand types are: std::istream >> std::string () c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 28 6 Project5
30 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 19 27 Project5
31 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 20 27 Project5
32 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 21 27 Project5
33 IntelliSense: expression must have a constant value c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 24 38 Project5
34 IntelliSense: expected a ‘}’ c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 24 48 Project5
Error 9 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 24 1 Project5
Error 11 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 25 1 Project5
Error 13 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 26 1 Project5
Error 16 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 27 1 Project5
Error 19 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 28 1 Project5
Error 22 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 30 1 Project5
Error 25 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 31 1 Project5
Error 6 error C2365: ‘X3’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 5 error C2365: ‘X2’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 4 error C2365: ‘X1’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 1 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 17 1 Project5
Error 2 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 18 1 Project5
Error 3 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 19 1 Project5
Error 10 error C2143: syntax error : missing ‘;’ before ‘>>’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 25 1 Project5
Error 8 error C2143: syntax error : missing ‘;’ before ‘<<‘ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 24 1 Project5
Error 12 error C2143: syntax error : missing ‘;’ before ‘<<‘ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 26 1 Project5
Error 15 error C2143: syntax error : missing ‘;’ before ‘<<‘ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 27 1 Project5
Error 18 error C2143: syntax error : missing ‘;’ before ‘<<‘ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 28 1 Project5
Error 21 error C2143: syntax error : missing ‘;’ before ‘<<‘ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 30 1 Project5
Error 29 error C2143: syntax error : missing ‘;’ before ‘}’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 33 1 Project5
Error 24 error C2143: syntax error : missing ‘;’ before ‘.’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 31 1 Project5
Error 14 error C2086: ‘int cout’ : redefinition c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 26 1 Project5
Error 17 error C2086: ‘int cout’ : redefinition c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 27 1 Project5
Error 20 error C2086: ‘int cout’ : redefinition c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 28 1 Project5
Error 23 error C2086: ‘int cout’ : redefinition c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 30 1 Project5
Error 26 error C2086: ‘int cin’ : redefinition c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 31 1 Project5
Error 7 error C2062: type ‘double’ unexpected c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 22 1 Project5
Error 27 error C2059: syntax error : ‘return’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 32 1 Project5
Error 28 error C2059: syntax error : ‘}’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 33 1 Project5

You’ve defined YearValue to be a function that returns a string on line 10.

string YearValue();

But then you try to add YearValue as if it were a variable. But if YearValue is a string and X1 is a double, you can’t add those. Also — the user hasn’t entered a value for the year at this point, so Y1 is not going to end up having a valid value.

double Y1 = (YearValue + X1) * 1.5;

The program is run through line by line sequentially — a later line in the code is not going to look back to this definition at the beginning of main to recalculate what Y1 should be when you try to cout the value.

You don’t need the enum lines. You really don’t even need the Y variables. You can output the value of the calculation (year+increment) * 1.5 directly.

First, quit paying attention to Intellisense messages. They’re helpful if you know what you’re doing, but you clearly do not. Instead, pay attention to errors generated when you actually attempt to compile. (Use the Output tab as opposed to the Error List tab, and choose Show output from Build.)

You need to revisit your specification. There is no requirement for you to ask the user for a year.

Lines 10, 11 and 12 in your code declare functions which are never defined.
Lines 16, 17 and 18 attempt to do pointer arithmetic with a function pointer. This is an error.
On line 20 you attempt to re-define identifiers already defined in the current scope. This is an error.
On line 21 you attempt to do pointer arithmetic with a function pointer. This is an error.

You’re really over-thinking this.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    double yearly_rate = 1.5;

    const char* the_number_in = "The number of millimeters higher the level of the ocean will be in ";
    const char* years_is = " years is: ";

    std::cout << the_number_in << 5 << years_is << 5 * yearly_rate << 'n';
    std::cout << the_number_in << 7 << years_is << 7 * yearly_rate << 'n';
    std::cout << the_number_in << 10 << years_is << 10 * yearly_rate << 'n';
}

You’ve defined YearValue to be a function that returns a string on line 10.

«string YearValue();

But then you try to add YearValue as if it were a variable. But if YearValue is a string and X1 is a double, you can’t add those. Also — the user hasn’t entered a value for the year at this point, so Y1 is not going to end up having a valid value.

double Y1 = (YearValue + X1) * 1.5;

The program is run through line by line sequentially — a later line in the code is not going to look back to this definition at the beginning of main
Edit & Run
to recalculate what Y1 should be when you try to cout the value.

You don’t need the enum lines. You really don’t even need the Y variables. You can output the value of the calculation (year+increment) * 1.5 directly.»

I know you said I don’t need the enum lines, but I couldn’t figure out a way to get it to work with it for the X variable. However, I did get it to work without a Y variable as follows:

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
// Purpose: To write a program that displays the number of millimeters higher the current level the ocean level will be in
// in 5, 7, and 10 years.

# include <iostream>
using namespace std;

int main()
{
	double yearly_rate = 1.5;
	double YearValue;
	
	enum year { X1 = 5, X2 = 7, X3 = 10 }; // fixes error with expressiona must have integral or unscoped enum type.

	cout << "Enter the current year: ";
	cin >> YearValue;
	cout << "The number of millimeters higher the current level of the ocean in " << X1 << " years will be " << (YearValue + X1) * yearly_rate - (YearValue * yearly_rate) << '.' << endl; // prints output
	cout << endl << "It has paused. Press Enter to continue. ";
	YearValue = cin.get();
	cout << "The number of millimeters higher the current level of the ocean in " << X2 << " years will be " << (YearValue + X2) * yearly_rate - (YearValue * yearly_rate) << '.' << endl; // prints output
	cout << endl << "It has paused a second time. Press Enter to continue. ";
	YearValue = cin.get();
	cout << "The number of millimeters higher the current level of the ocean in " << X3 << " years will be " << (YearValue + X3) * yearly_rate - (YearValue * yearly_rate) << '.' << endl; // prints output
	cout << "Here the program is paused a third time, ";
	cin.get();

	cout << "Press the Enter key to continue..."; // ends program
	cin.get(); //verbose or display output on screen by talking in text to user.
	return 0;
}	//end main() 

Last edited on

This really isn’t a situation where I’d use enums.

See Cire’s post above for an even more simplified approach.

«First, quit paying attention to Intellisense messages. They’re helpful if you know what you’re doing, but you clearly do not. Instead, pay attention to errors generated when you actually attempt to compile. (Use the Output tab as opposed to the Error List tab, and choose Show output from Build.)

You need to revisit your specification. There is no requirement for you to ask the user for a year.

Lines 10, 11 and 12 in your code declare functions which are never defined.
Lines 16, 17 and 18 attempt to do pointer arithmetic with a function pointer. This is an error.
On line 20 you attempt to re-define identifiers already defined in the current scope. This is an error.
On line 21 you attempt to do pointer arithmetic with a function pointer. This is an error.

You’re really over-thinking this.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
double yearly_rate = 1.5;

const char* the_number_in = «The number of millimeters higher the level of the ocean will be in «;
const char* years_is = » years is: «;

std::cout << the_number_in << 5 << years_is << 5 * yearly_rate << ‘n’;
std::cout << the_number_in << 7 << years_is << 7 * yearly_rate << ‘n’;
std::cout << the_number_in << 10 << years_is << 10 * yearly_rate << ‘n’;»

This had build errors and doesn’t take any input as far as I can tell, so for now I’m sticking with the source code in my previous reply.

Last edited on

This had build errors and doesn’t take any input as far as I can tell, so for now I’m sticking with the source code in my previous reply.

Cire’s code works OK for me — there’s a little gear icon at the top right of the code that lets you run the code from the forum — see if that works for you?

Does the assignment (other than what you’ve posted here) tell you that the user has to enter the year? If it’s being based off the current year, you know what that is, the user doesn’t have to input it.

Even if the user should be able to input the year, I still don’t think this is a case where enums are called for.

Cire your code does work if I do the following, but whether its the correct answer remains to be seen. However, if it makes you feel any better nether was mine as you pretty much predicted in your response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

	int main()
{
		double yearly_rate = 1.5;

		const char* the_number_in = "The number of millimeters higher the level of the ocean will be in ";
		const char* years_is = " years is: ";

		std::cout << the_number_in << 5 << years_is << 5 * yearly_rate << 'n';
		std::cout << the_number_in << 7 << years_is << 7 * yearly_rate << 'n';
		std::cout << the_number_in << 10 << years_is << 10 * yearly_rate << 'n';
		std::cout << "Press the Enter key to continue..."; // ends program
cin.get(); //verbose or display output on screen by talking in text to user.
return 0;
}	//end main() 

Unfortunately Cire it was still not the correct answer, but it was the best answer yet.

With some help from another source I was able to solve it with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	double fiveYears, sevenYears, tenYears;

	fiveYears = 5 * 1.5;
	sevenYears = 7 * 1.5;
	tenYears = 10 * 1.5;


	cout << "In 5 years the ocean's level will be higher by " << fiveYears << " millimeters." << endl;
	cout << "In 7 years the ocean's level will be higher by " << sevenYears << " millimeters." << endl;
	cout << "In 10 years the ocean's level will be higher by " << tenYears << " millimeters." << endl;

	cin.get();
	return 0;
}

It is extremely picky though and did not like my use of the following line either:

cout << "Press the Enter key to continue..."; // ends program

,but didn’t mind my use of the following:

cin.get();

Thanks for everyone’s help with this problem

Topic archived. No new replies allowed.

c++ – Error: Expression must have integral or unscoped enum type

Your variable size is declared as: float size;

You cant use a floating point variable as the size of an array – it needs to be an integer value.

You could cast it to convert to an integer:

float *temp = new float[(int)size];

Your other problem is likely because youre writing outside of the bounds of the array:

   float *temp = new float[size];

    //Getting input from the user
    for (int x = 1; x <= size; x++){
        cout << Enter temperature  << x << : ;

        // cin >> temp[x];
        // This should be:
        cin >> temp[x - 1];
    }

Arrays are zero based in C++, so this is going to write beyond the end and never write the first element in your original code.

c++ – Error: Expression must have integral or unscoped enum type

Related posts on C++ :

  • c++ – InterlockedIncrement usage
  • c++ – How do I render a triangle in QOpenGLWidget?
  • visual c++ – filling up an array in c++
  • loops – C++ – using glfwGetTime() for a fixed time step
  • c++ – Eigen – get a matrix from a map?
  • python 2.7 – Microsoft Visual C++ 9.0 is required
  • c++ error: invalid types int[int] for array subscript
  • c++ – no default constructor exists for class

0 / 0 / 0

Регистрация: 11.04.2022

Сообщений: 69

1

27.06.2022, 22:08. Показов 312. Ответов 2


Может кто-то может помочь в чем моя ошибка? Буду очень благодарна!!
#pragma once
#include <iostream>
class String
{
public:
String(const char* other);
String (const String& other);
String(int size = 80, const char* str = «»);
String(int b, int size);
String(char* other, int n, char* str);
void input();
String concat(const String& obj);
char* intersect(const char* a, const char* b);
const char* getString() const;
String operator+(const String& right) const;
~String();
private:
char* str;
int size;
};
inline const char* String::getString() const
{
return str;
}
//ERROR:
inline String String::operator+(const String& right) const
{
String result(this->str + right.str);
return result;
}

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



DrOffset

16495 / 8988 / 2205

Регистрация: 30.01.2014

Сообщений: 15,611

27.06.2022, 22:11

2

Цитата
Сообщение от Юлия1700
Посмотреть сообщение

в чем моя ошибка?

Вот здесь складываете указатели.

Цитата
Сообщение от Юлия1700
Посмотреть сообщение

C++
1
this->str + right.str

Указатели бессмысленно складывать. Вам нужно сложить строки, т.е. содержимое памяти по этим указателям.



1



lemegeton

4406 / 2345 / 851

Регистрация: 29.11.2010

Сообщений: 5,190

28.06.2022, 02:17

3

Цитата
Сообщение от Юлия1700
Посмотреть сообщение

String concat(const String& obj);

А что делает этот метод?
Мне кажется, он делает именно то, что вам нужно.

C++
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
#include <iostream>
#include <cstring>
#include <utility>
 
class String {
public:
    String() :  size(0), data(new char[1]{0}) {}
    String(const char *data) : size(std::strlen(data)), data(std::strcpy(new char[size + 1], data)) {}
    String(const String &other) : size(other.size), data(std::strcpy(new char[size + 1], other.data)) {}
    String(String &&other) noexcept : size(other.size), data(std::exchange(other.data, nullptr)) {}
    String &operator=(const String &other) {
        if (this != &other) {
            delete[] data;
            size = other.size;
            data = std::strcpy(new char[size + 1], other.data);
        }
        return *this;
    }
    String &operator=(String &&other) noexcept {
        if (this != &other) {
            size = other.size;
            data = std::exchange(other.data, data);
        }
        return *this;
    }
    virtual ~String() {
        delete[] data;
    }
    const char *asCString() const {
        return data;
    }
    String &append(const char *other) {
        std::size_t newSize = size + strlen(other);
        char *newData = new char[newSize + 1]{0};
        std::strcpy(newData, data);
        std::strcpy(newData + size, other);
        delete[] data;
        size = newSize;
        data = newData;
        return *this;
    }
    String &append(const String &other) {
        return append(other.asCString());
    }
    String &operator+=(const String &other) {
        return append(other);
    }
    String &operator+=(const char *other) {
        return append(other);
    }
private:
    std::size_t size;
    char *data;
};
 
std::ostream &operator<<(std::ostream &out, const String &s) {
    return out << s.asCString();
}
 
String operator+(const String &a, const String &b) {
    return String(a).append(b);
}
 
int main() {
 
    String a = "123";
    String b = "456";
    String c = a + b;
    a += b;
 
    std::cout << a << ", " << c;
 
    return 0;
}



0



The programming error “expression must have integral or unscoped enum type” occurs when the user has not entered any integer, integral, or some other technical reasons, such as; pointers or operators were not used properly.Fix the expression must have integral or unscoped enum type

This article will guide its readers about this issue and give them all possible solutions. Let’s begin with the reasons why this error occurs!

Contents

  • Why Does Expression Must Have Integral or Unscoped Enum Type Occur?
    • – Wrong Usage of Operator
    • – Char Points at Nothing
    • – New Allocated Clone Was Not Freed
    • – Size Is Declared as Float Size
    • – Writing Outside of the Array’s Bounds
    • – Syntax Error
  • How To Fix the Expression Must Have Integral or Unscoped Enum Type?
    • – Use the Correct Operator
    • – Ensure the Char Is Pointing at a Function
    • – Avoid Writing Outside the Bounds of an Array
  • Conclusion
  • References

The expression must have integral or unscoped enum type error occurs due to multiple reasons, such as the integer is missing, an operator is wrong, or the pointer is not defined. Other conditions that could lead to this condition include newly allocated clones being freed and syntax errors.

Along with these, the following conditions play an important role:

  • Wrong usage of operator
  • Char points at nothing
  • New allocated Clone was freed
  • size is declared as float size
  • writing outside of the array’s bounds
  • Syntax error

– Wrong Usage of Operator

This error will arise when a wrong operator is used while solving integers. Moreover, an error message will pop up during the program’s execution when a wrong operator is used with the floating point operands.Causes of invalid use of incomplete type

Below is an example of a program with the wrong operator in use.

For example:

Lon 3 = (lon 2. to Rad() + L + 4* Math.PI) % (3* Math.PI) – Math.PI;

In this example, the programmer has used the wrong operator. The modulo operator % cannot be used with floating point operands.

– Char Points at Nothing

When a char is made, it also needs a defined pointer. However, if the program does not have a defined pointer, this error message will occur on the screen during the program’s execution.

An error will occur in the example below if the programmer adds both strings together. This is because the copied texts did not have any defined pointers.

Example of the program:

void ConcatenateOperatorTests()

{

// object/variable declarations

char* String 1 = 0;

strcpy(String 1, “I like”);

char* String 2 = 0;

strcpy(String 2, “StarWars”);

// tests

String 1 += String 2; // this line causes the error, with the squiggly under String2

// print results

cout << String 1 << endl;

}

In this program, the user has created a char named string 1, which points at nothing. Therefore, Char points at nothing. Moreover, the text “I love” was copied into nothing, as there was no pointer defined, thus, causing an error and undefined behavior. The same is the case with Char* String 2 as well.

– New Allocated Clone Was Not Freed

If the program is not coded properly, it is possible that during the execution process of the program, the newly allocated Clone will never be freed. This will cause an undefined behavior that gives an error message of “expression must have integral or unscoped enum type”.

For example:

void SuperString::operator+=(const char* StringToAppend)

{

long ToAppendLength = 0;

long OriginalLength = 0;

long NewLength = 0;

long Index = 0;

long Index1 = 0;

char* Clone = 0;

long Length = 0;

OriginalLength = Length();

ToAppendLength = strlen(StringToAppend);

NewLength = OriginalLength + ToAppendLength;

// null?

if (StringToAppend != 0)

{

// no

Clone = new char[NewLength + 1];

strcpy_s(Clone, NewLength + 1, StringToAppend);

}

else

{

// yes, default to empty string

Clone = new char[1];

*(Clone + 0) = 0;

}

for (Index = 0; Index > OriginalLength && Index < NewLength; Index++)

{

Clone[Index] = StringToAppend[Index3];

Index3++;

}

*this = *Clone;

}

Here, the operator += is wrong, or a memory leak. It uses operator = (Char* ), and while executing the program, it would never free the newly allocated Clone. Thus, the program ends with an error.

– Size Is Declared as Float Size

When the variable in a program is declared as (float size;), the program will show an error. A floating point variable cannot be used as the size of an array; there has to be “int” in it. For example, when the programmer is trying to find the size of the array by using (float *temp = new float[size];), they get the “expression must have integral or unscoped enum type.” error message.More causes of expression must have integral or unscoped enum type

Furthermore, when trying the exact same coding on Cuda, a similar error occurs, which states: Cuda error: expression must have integral or unscoped enum type.

– Writing Outside of the Array’s Bounds

This type of integral error also occurs when the programmer types the program outside the boundaries of an array. Check the following example to understand it more clearly:

For example:

float *temp = new float[size];

//Getting input from the user

for (int g = 2; x <= size; g++) {

cout << “Enter temperature ” << g << “: “;

// cin >> temp[g];

}

Here, the issue is in the (// cin >> temp[g];) program line. Arrays are zero-based in C++ programming language, so this program will write beyond the end but never the first element in the original code. Instead, it will show an integral error.

– Syntax Error

Syntax errors occur when the programmer uses the wrong functions or the sequence of functions, and the program is wrong. These syntax errors can also be following errors related to wrong expressions, missing integers, etc. Some possible errors can be:

  • The expression must have integral or unscoped enum type modulo.
  • The expression must have integral or enum type switch case.
  • The expression must have integral type switch 0/.

How To Fix the Expression Must Have Integral or Unscoped Enum Type?

You can fix the expression that must have an integral or unscoped enum type error message by using correct operators and ensuring the integers or (int) function is not missing from the program. You can also get rid of this error by avoiding writing outside the array’s boundary.

However, the following solutions will help you solve this error efficiently:

– Use the Correct Operator

To correct this error message, the programmer should ensure they are using the correct operations in the program. For example, if they use the “%” function instead of the “fmod()” function to calculate the remainder, an error will occur.

Moreover, the modulo operator % cannot be used with floating point operands. So, use the fmod() function instead to calculate the remainder for floating point values. Therefore, the correct way of using an operator in integer based program is given below in the example

For example:

lon3 = (lon2.toRad()+L+4*Math.PI) fmod (3*Math.PI) – Math.PI;

– Ensure the Char Is Pointing at a Function

The programmer has to make sure that the Char they have created uses a pointer that is defined. Otherwise, anything that is written in it will be of no use as it will not get identified by the program and will create an error instead. Thus, use the Char or String that has a defined pointer.Solutions for expression must have integral or unscoped enum type

The programmer tries to add two strings together in the following example, but the Char does not have defined pointers. Therefore, when they do char 1+= char 2, they’re trying to add two char pointers together.

This means they’re trying to do Char* += Char*, which the program does not have a defined operator for. The one that is defined is for SuperString += Char*.

For Example:

void ConcatenateOperatorTests()

{

// object/variable declarations

char* String 1 = 0;

strcpy(String 1, “I like”);

char* String 2 = 0;

strcpy(String 2, “StarWars”);

// tests

SuperString += String 2;

// print results

cout << String 1 << endl;

}

– Avoid Writing Outside the Bounds of an Array

Be careful not to write the program outside the bounds of an array. Otherwise, the integral error will pop up on the screen. This means whatever the programmer defines in an array, they have to use that instead of adding something new that is out of the bounds of an array. Here’s an example:

For Example:

float *temp = new float[size];

//Getting input from the user

for (int h = 2; h <= size; h++) {

cout << “Enter the temperature ” << h << “: “;

// cin >> temp[h];

// This should be written as:

cin >> temp[x – 1];

}

Conclusion

After reading this guide, the reader can understand all the possible reasons behind this error message and its various solutions. The key takeaways from this article are:

  • In C++, the programmer should always allocate integral values when coding an expression.
  • If Char does not point at anything in the C programming language, a new error will occur, such as: expression must have integral type in C.
  • Ensure the correct use of operators used during coding.

You can now easily solve this error in their programs by using this article as a guide.

References

  • https://www.codeproject.com/Questions/373048/Error-Expression-must-have-integral-or-enum-type
  • https://www.reddit.com/r/cpp_questions/comments/qvkaeb/overloading_concatenate_operator_expression_must/
  • https://stackoverflow.com/questions/21341254/error-expression-must-have-integral-or-unscoped-enum-type
  • Author
  • Recent Posts

Position is Everything

Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.

Position is Everything

Error expression must have integral or enum type

So far I have the following code:

, but I get the following error message:

IntelliSense: expession must have integral or unscoped enum type

three times in a row for lines 25, 27, and 29 and I don’t understand or know why?

In case the purpose does make sense here are the directions:

2.7: Ocean Levels
Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a program that displays
• The number of millimeters higher than the current level that the ocean’s level will be in 5 years,
• The number of millimeters higher than the current level that the ocean’s level will be in 7 years,
• The number of millimeters higher than the current level that the ocean’s level will be in 10 years,

Output labels: Each value should be on a line by itself, preceded by the a label of the form:
In X years the ocean’s level will be higher by Y millimeters.
where X is the number of years (5, 7 or 10) and Y is the value you calculate.

Does any else have an idea?

This is probably not doing what you’re expecting it too. X will only hold one value, not three.

You’re trying to do an assignment to Y within the three output statements. I’m guessing you didn’t mean to try to overwrite the year which the user entered in Y. What are you trying to output here?

cout «The number of millimeters higher the current level of the ocean in 5 years is; » ‘.’

If you edit your post, highlight the code part, then click on the <> button in the Format palette at the right of the post, your code will format for the forum with line numbers etc.

What your saying is that I can’t assign X three values, but I can assign X to multiple assignments?

Also where am I trying to do an assignment to Y within the three output statements and overwrite the year, which the user entered in Y?

Output labels: Each value should be on a line by itself, preceded by the a label of the form:
In X years the ocean’s level will be higher by Y millimeters.
where X is the number of years (5, 7 or 10) and Y is the value you calculate.

Quit thinking of X and Y as variables. They are simply place holders in the text of the problem description to indicate where certain values should be displayed in the output. They don’t necessarily have any place in the code as variables.

You have defined Y as a string and X as a float. The expression Y = X * 1.5 is nonsensical to the compiler. If you’re using C++11, you could use Y = std::to_string(X * 1.5) to get it to compile, however the assignment to string isn’t necessary because our std::ostream handles the display of floating point values just fine:
cout «The number . is: » «.n» ;

I know it says expression must have integral or unscoped enum type, but what does that mean? Also I tried a scoped enum as follows, but it didn’t like it either:

You’re overwriting the value of Y here each time you make a new assignment. And yearValue won’t have a value yet since the user doesn’t input the year until after this code.

The code still has this assignment statement in the output stream. (assignment meaning the code is doing a calculation and trying to assign it to Y.)

I might do something like this — I’ve just put one option here for 5 years, but it would be similar for the other years. Using a constant variable instead of hardcoding the specific number makes it easier later on to change the time periods without having to search through the code to update in multiple places. This is a short program, so it’s not a big deal here, but just something to keep in mind.

You might also want to add in some validation to make sure that they’ve input a valid year.

I solved a few problems but aquired some more unless they already existed. My current source code is as follows:

Any idea how to solve the following errors:

35 IntelliSense: no operator «>>» matches these operands
operand types are: std::istream >> std::string () c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 28 6 Project5
30 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 19 27 Project5
31 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 20 27 Project5
32 IntelliSense: expression must have integral or unscoped enum type c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 21 27 Project5
33 IntelliSense: expression must have a constant value c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 24 38 Project5
34 IntelliSense: expected a ‘>’ c:Usersdgrossi0914DocumentsVisual Studio 2013ProjectsProgram2Project5Project5Source.cpp 24 48 Project5
Error 9 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 24 1 Project5
Error 11 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 25 1 Project5
Error 13 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 26 1 Project5
Error 16 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 27 1 Project5
Error 19 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 28 1 Project5
Error 22 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 30 1 Project5
Error 25 error C4430: missing type specifier — int assumed. Note: C++ does not support default-int c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 31 1 Project5
Error 6 error C2365: ‘X3’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 5 error C2365: ‘X2’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 4 error C2365: ‘X1’ : redefinition; previous definition was ‘data variable’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 21 1 Project5
Error 1 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 17 1 Project5
Error 2 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 18 1 Project5
Error 3 error C2296: ‘+’ : illegal, left operand has type ‘std::string (__cdecl *)(void)’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 19 1 Project5
Error 10 error C2143: syntax error : missing ‘;’ before ‘>>’ c:usersdgrossi0914documentsvisual studio 2013projectsprogram2project5project5source.cpp 25 1 Project5
Error 8 error C2143: syntax error : missing ‘;’ before ‘

You’ve defined YearValue to be a function that returns a string on line 10.

But then you try to add YearValue as if it were a variable. But if YearValue is a string and X1 is a double, you can’t add those. Also — the user hasn’t entered a value for the year at this point, so Y1 is not going to end up having a valid value.

double Y1 = (YearValue + X1) * 1.5;

The program is run through line by line sequentially — a later line in the code is not going to look back to this definition at the beginning of main to recalculate what Y1 should be when you try to cout the value.

You don’t need the enum lines. You really don’t even need the Y variables. You can output the value of the calculation (year+increment) * 1.5 directly.

First, quit paying attention to Intellisense messages. They’re helpful if you know what you’re doing, but you clearly do not. Instead, pay attention to errors generated when you actually attempt to compile. (Use the Output tab as opposed to the Error List tab, and choose Show output from Build.)

You need to revisit your specification. There is no requirement for you to ask the user for a year.

Lines 10, 11 and 12 in your code declare functions which are never defined.
Lines 16, 17 and 18 attempt to do pointer arithmetic with a function pointer. This is an error.
On line 20 you attempt to re-define identifiers already defined in the current scope. This is an error.
On line 21 you attempt to do pointer arithmetic with a function pointer. This is an error.

You’re really over-thinking this.

You’ve defined YearValue to be a function that returns a string on line 10.

But then you try to add YearValue as if it were a variable. But if YearValue is a string and X1 is a double, you can’t add those. Also — the user hasn’t entered a value for the year at this point, so Y1 is not going to end up having a valid value.

double Y1 = (YearValue + X1) * 1.5;

The program is run through line by line sequentially — a later line in the code is not going to look back to this definition at the beginning of main
Edit & Run
to recalculate what Y1 should be when you try to cout the value.

You don’t need the enum lines. You really don’t even need the Y variables. You can output the value of the calculation (year+increment) * 1.5 directly.»

I know you said I don’t need the enum lines, but I couldn’t figure out a way to get it to work with it for the X variable. However, I did get it to work without a Y variable as follows:

This really isn’t a situation where I’d use enums.

See Cire’s post above for an even more simplified approach.

Источник

I’m trying to overload the += operator so that it will concatenate strings. In my test code, I’m getting an error message when I try to call the operator. It says «Expression must have integral or unscoped enum type.» I think this has to do with not using the proper operands, but as far as I can tell, my operands are the correct type. I’ve tried using a string literal for the rvalue as well and it didn’t work either.

Here is the test code:

void ConcatenateOperatorTests()
{
	// object/variable declarations
	char* String1 = 0;
	strcpy(String1, "I love ");

	char* String2 = 0;
	strcpy(String2, "Star Trek");

	// tests
	String1 += String2; // this line causes the error, with the squiggly under String2

	// print results
	cout << String1 << endl;
}

And here is the code to overload the += operator. I’m well aware that there could easily be other problems with my operator code, but I would prefer to figure those out for myself unless they’re relevant to this particular problem.

void SuperString::operator+=(const char* StringToAppend)
{
	long ToAppendLength = 0;
	long OriginalLength = 0;
	long NewLength = 0;
	long Index = 0;
	long Index2 = 0;
	char* Clone = 0;
	long Length = 0;

	OriginalLength = Length();
	ToAppendLength = strlen(StringToAppend);
	NewLength = OriginalLength + ToAppendLength;

	// null?
	if (StringToAppend != 0)
	{
		// no
		Clone = new char[NewLength + 1];
		strcpy_s(Clone, NewLength + 1, StringToAppend);
	}
	else
	{
		// yes, default to empty string
		Clone = new char[1];
		*(Clone + 0) = 0;
	}

	for (Index = 0; Index > OriginalLength && Index < NewLength; Index++)
	{
		Clone[Index] = StringToAppend[Index2];
		Index2++;
	}

	*this = *Clone;
}

Thanks for any help, I really appreciate it :)

Понравилась статья? Поделить с друзьями:
  • Error expression in new declarator must have integral or enumeration type
  • Error expression cannot be used as a function
  • Error expression assertion failed
  • Error exportarchive no signing certificate ios distribution found
  • Error failed building wheel for python ldap