Possible class/header-file problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brixton
    New Member
    • Nov 2006
    • 35

    #1

    Possible class/header-file problem

    Hello,

    I'm new to C++ and the way to work with classes and header files and so on.
    Compiling the following code I get this error (marked in the code):
    "expected primary-expression before ']' token

    -- main.cpp
    Code:
    #include <iostream>
    #include "air.h"
    
    using namespace std;
    
    air thisair;
    
    int main() {
            
        thisair.init();
        cout << thisair.getDensity(4000, 100) << endl; 
        while(true) {}
        return 0;
        
    }
    -- air.cpp
    Code:
    #include <iostream>
    #include "air.h"
    
    void air::init() {
         
         for (int i=0; i<60; i++) { heightIntervals[i] = 250*i; }
         
         currentDensity = 1.2275;
    
         kValues[] = {lots of values separated by commas in here}; 
          *** ERROR APPEARS FOR THE LINE ABOVE***
    
    }
    
    double air::getDensity(double height, double heightChange) {
         
         for (int i=0; i<59; i++) {
             if ((height >= heightIntervals[i]) && (height < heightIntervals[i+1])) {
                         currentDensity = currentDensity + kValues[i]*heightChange;
             }
         }
         
         if (height > heightIntervals[59]) { currentDensity = 0; }
         
         return currentDensity;
    }
    -- air.h
    Code:
    class air {
    
          double currentDensity;                       
          double heightIntervals[60];
          double kValues[60];
    
          public:
               void init();
               double getDensity(double, double);
    };
    Now, I've been moving around the definitions etc. between h-files and cpp-files so it's possible the error is related to the way I've structured the whole thing.
    I think it's not the array itself that is the issue since I've initiated arrays like that before.

    Thanks in advance
    // Joakim, Sweden
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I don't know if this will help, but I've never been able to get classes to work with a seperate header file and implementation file. Try moving the definitions into air.h, like so:

    Code:
    class air {
       private:
          whatever
          whatever
          whatever
    
       public:
          whatever
          whatever
    };
    
    void air::whatever() {
       blah
       blah
       blah
       return;
    }
    ...

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      if the values in kValues[] are common to all instances of Air could you not make it a static data member and initialise it in the header, e.g.
      Code:
      class air {
      
            double currentDensity;                       
            double heightIntervals[60];
            static double kValues[60];
            public:
                 void init();
                 double getDensity(double, double);
      };
      
      double air::kValues[]={1.0,2.0,3.0,4.0,5.0};

      Comment

      • brixton
        New Member
        • Nov 2006
        • 35

        #4
        Thanks both, I went with the first suggestion and threw away air.cpp (moved all its content to air.h)

        However, now I'm getting "[Linker error] undefined reference to 'air::init()' (and the same for the other function called) in main.cpp?

        Edit: the static-thing with the array did not work :(

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by brixton
          Thanks both, I went with the first suggestion and threw away air.cpp (moved all its content to air.h)

          However, now I'm getting "[Linker error] undefined reference to 'air::init()' (and the same for the other function called) in main.cpp?

          Edit: the static-thing with the array did not work :(
          Did you specify both files to the compiler? e.g. using GNU C++
          g++ main.cpp air.cpp

          the files below (compiled using g++ as above) when run gave (clearly the kValues are wrong!)
          1.2275

          Code:
          // main.cpp
          
          #include <iostream>
          #include "air.h"
          
          using namespace std;
          
          air thisair;
          
          int main() {
                  
              thisair.init();
              cout << thisair.getDensity(4000, 100) << endl; 
              cin.get();
              return 0; 
          }
          Code:
          // air.cpp
          
          #include <iostream>
          #include "air.h"
          
          double air::kValues[]={1.0,2.0,3.0,4.0,5.0};
          
          void air::init()
          {     
               for (int i=0; i<60; i++) { heightIntervals[i] = 250*i; }
               currentDensity = 1.2275;
          }
          
          double air::getDensity(double height, double heightChange) {
               
               for (int i=0; i<59; i++) {
                   if ((height >= heightIntervals[i]) && (height < heightIntervals[i+1])) {
                               currentDensity = currentDensity + kValues[i]*heightChange;
                   }
               }
               
               if (height > heightIntervals[59]) { currentDensity = 0; }
               
               return currentDensity;
          }
          Code:
          // air.h
          
          class air {
          
                double currentDensity;                       
                double heightIntervals[60];
                static double kValues[60];
                public:
                     void init();
                     double getDensity(double, double);
          };

          Comment

          • brixton
            New Member
            • Nov 2006
            • 35

            #6
            I'm using DevC++ so I'm assuming it does that as long as the files are in the same "project"?

            Well, you're getting that value since kValues are given too few values in your case (I give it 60 values but I didn't feel like writing all that in the code example here ;)).

            But if I just use air.h and main.cpp as stated above, why is it giving me a linking error?

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by brixton
              I'm using DevC++ so I'm assuming it does that as long as the files are in the same "project"?

              Well, you're getting that value since kValues are given too few values in your case (I give it 60 values but I didn't feel like writing all that in the code example here ;)).

              But if I just use air.h and main.cpp as stated above, why is it giving me a linking error?
              I have run the version in my last post under DEV-C++ OK

              I created a new console application project, added main.cpp and air.cpp to it, compiled and ran, gave same result 1.2275

              Comment

              • brixton
                New Member
                • Nov 2006
                • 35

                #8
                I changed my code as yours and got it to work when I assigned the correct amount of elements in kValues.

                Thanks a lot for the help.

                Comment

                Working...