C++: I'm using Microsoft Visual Studio 2012 and I am not sure what to do about these two errors:

             2  IntelliSense: identifier "x" is undefined   
            Error   1   error C2065: 'x' : undeclared identifier

Prompt: Make program that reads 20 integers, display that array, and find min, max, sum, and avg.
One method named "read from user file" which will ask the user to input a filename and will then read from 1 to 20 integers from that file, but not calculate any statistics. Second method will display the values read in, in the order they were found in the file. Third method will calculate the statistics and store these in the appropriately named variables that are also to be declared in the class. Finally a fourth method will display those statistics from those variables. Your int main function will create an object variable of the Numbers class type. It will then use that object to call those four methods, one after another, in order to demonstrate that the class and it's methods work correctly. No global variables should be used in this program. The Numbers class will be defined in its own .h file, and will use the Ifndef Trick. Make no system calls. User Interface Note that no debugging information (useful for testing a program's internal states) should be output in the submitted program

//numbers1.cpp//

#include <iostream>
#include <fstream>
#include <string>
#include "numbers1.h"
#define x 20

using namespace std;


void Numbers::read_from_user_file(int m_arr)
{
ifstream f("Numbers.txt");
if(f.is_open())
{
cout << "File Open successful!" << endl;
while(!f.eof() && i<a[x])
{
f>>a[i];
i++;
}a[i-1] = '\0';
}
}
void Numbers::displays(int m_arr)
{
for(i=0; i<m_arr; i++)
cout<<a[i]<<" ";
}
void Numbers::m_min(int m_arr)
{
int min=a[0];
for(i=1; i<m_arr; i++)
if(a[i]<min)
min=a[i];
cout<<"\nMinimum= "<<min;
}
void Numbers::m_max(int m_arr)
{
int max=a[0];
for(i=1; i<m_arr; i++)
if(a[i]>max)
max=a[i];
cout<<"\nMaximum= "<<max;
}
void Numbers::sum_tot(int m_arr)
{
int sum=0;
for(i=0; i<m_arr; i++)
sum+=a[i];
cout<<"\nSum= "<<sum;
}
void Numbers::avg(int m_arr)
{
   double avg=0;
   for (i=0; i<m_arr; i++)
       avg+=a[i];
   avg/=m_arr;
   cout << "\nAverage= " << avg;
}
int main()
{
Numbers obj;
fstream f;
   int m_arr=20;
cout<<"\nNumbers: \n";
obj.read_from_user_file(m_arr);
obj.displays(m_arr);
obj.m_min(m_arr);
obj.m_max(m_arr);
obj.sum_tot(m_arr);
   obj.avg(m_arr);
cout<<endl;
system("pause");
   return 0;
}

//numbers1.h

#ifndef NUMBERS_H
#define NUMBERS_H
class Numbers
{
private:
int i, a[x];
public:
void m_min(int);
void m_max(int);
void sum_tot(int);
       void avg(int);
void read_from_user_file(int);
void displays(int);
};
#endif

**//numbers1.txt// **
20 13 15 16 18 12 21 34 54 6 25 29 53 36 42 66 41 14 85 30

Dani AI

Generated

The two compiler messages come from the same root cause: the header is compiled before the macro x is seen, so int a[x]; in the header is parsed with x undefined. That produces IntelliSense “identifier 'x' is undefined” and the C2065 error. Moving #define x 20 above the #include fixes it, but better options are to put a named constant or constexpr in the header, or to use std::vector<int> so the size is not hard-coded. As suggested, prefer a constant over a macro; as suggested, consider an STL container for flexibility.

Practical cleanups that will stop other problems seen in the thread:

  • Do not keep a loop index like i as a class field. Use local loop variables and keep a count member for how many numbers were actually read.
  • Never write a[i-1] = '\0'; for an int array. That’s a C-string idiom and is wrong here.
  • Avoid while(!f.eof()). Use while (in >> value && count < SIZE) or vector::push_back.
  • Check count > 0 before computing min/max/avg to avoid undefined behavior and divide-by-zero.
  • Don’t put using namespace std; in a header and avoid system("pause") (the assignment forbids system calls).

Example skeleton (place in the header; this is different from the code posted in the thread):

#ifndef NUMBERS1_H
#define NUMBERS1_H

#include <cstddef>

class Numbers {
public:
    static constexpr std::size_t SIZE = 20;
    void read_from_user_file();   // prompts for filename, fills array, sets count
    void displays() const;
    void compute_stats();         // sets min_, max_, sum_, avg_
    void display_stats() const;
private:
    int a[SIZE];
    std::size_t count = 0;
    int min_ = 0, max_ = 0;
    long long sum_ = 0;
    double avg_ = 0.0;
};

#endif

For the “IntelliSense: expected a declaration” message, common causes are an earlier preprocessor/macro problem, a stray token outside any declaration (missing semicolon or brace), or a BOM/non-ASCII character at the top of a source/header. After fixing the macro/constant and cleaning up the header (remove the i member, correct the read loop, remove the '\0' assignment), do a full clean/rebuild; that will usually clear the spurious IntelliSense/parse errors.

Recommended Answers

All 6 Replies

Did you try to put your #define x 20 in the .h file instead of the .cpp file?
I would rather use const here, instead of #define

x is not defined prior to the #include of numbers.h so it cannot yet be substituted at the statement:

int i, a[x];

in your header

You must either move your #define prior to the #include for numbers.h or, as suggested, put the #define inside the header itself.

My own preference would not be to use a statically allocated array but to use a container from the standard template library such as std::vector since then you don't need to recompile your program for files with differing numbers of numbers.

How would it look like if I used const cause I think I tried it but didn't work?
or
I think I tried moving #define inside header but it didn't change anthing, I don't know I did it properly or not but thats what happened.

I got that part actually now, thanks.

1   IntelliSense: expected a declaration    

I'm getting this error for text file though?

Try to be more specific, which line, what variable, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.