473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Possible class/header-file problem

35
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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "air.h"
  3.  
  4. using namespace std;
  5.  
  6. air thisair;
  7.  
  8. int main() {
  9.  
  10.     thisair.init();
  11.     cout << thisair.getDensity(4000, 100) << endl; 
  12.     while(true) {}
  13.     return 0;
  14.  
  15. }
  16.  
-- air.cpp
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "air.h"
  3.  
  4. void air::init() {
  5.  
  6.      for (int i=0; i<60; i++) { heightIntervals[i] = 250*i; }
  7.  
  8.      currentDensity = 1.2275;
  9.  
  10.      kValues[] = {lots of values separated by commas in here}; 
  11.       *** ERROR APPEARS FOR THE LINE ABOVE***
  12.  
  13. }
  14.  
  15. double air::getDensity(double height, double heightChange) {
  16.  
  17.      for (int i=0; i<59; i++) {
  18.          if ((height >= heightIntervals[i]) && (height < heightIntervals[i+1])) {
  19.                      currentDensity = currentDensity + kValues[i]*heightChange;
  20.          }
  21.      }
  22.  
  23.      if (height > heightIntervals[59]) { currentDensity = 0; }
  24.  
  25.      return currentDensity;
  26. }
  27.  
-- air.h
Expand|Select|Wrap|Line Numbers
  1. class air {
  2.  
  3.       double currentDensity;                       
  4.       double heightIntervals[60];
  5.       double kValues[60];
  6.  
  7.       public:
  8.            void init();
  9.            double getDensity(double, double);
  10. };
  11.  
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
Nov 18 '06 #1
7 2111
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. class air {
  2.    private:
  3.       whatever
  4.       whatever
  5.       whatever
  6.  
  7.    public:
  8.       whatever
  9.       whatever
  10. };
  11.  
  12. void air::whatever() {
  13.    blah
  14.    blah
  15.    blah
  16.    return;
  17. }
  18. ...
Nov 18 '06 #2
horace1
1,510 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. class air {
  2.  
  3.       double currentDensity;                       
  4.       double heightIntervals[60];
  5.       static double kValues[60];
  6.       public:
  7.            void init();
  8.            double getDensity(double, double);
  9. };
  10.  
  11. double air::kValues[]={1.0,2.0,3.0,4.0,5.0};
  12.  
Nov 18 '06 #3
brixton
35
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 :(
Nov 18 '06 #4
horace1
1,510 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. // main.cpp
  2.  
  3. #include <iostream>
  4. #include "air.h"
  5.  
  6. using namespace std;
  7.  
  8. air thisair;
  9.  
  10. int main() {
  11.  
  12.     thisair.init();
  13.     cout << thisair.getDensity(4000, 100) << endl; 
  14.     cin.get();
  15.     return 0; 
  16. }
  17.  
Expand|Select|Wrap|Line Numbers
  1. // air.cpp
  2.  
  3. #include <iostream>
  4. #include "air.h"
  5.  
  6. double air::kValues[]={1.0,2.0,3.0,4.0,5.0};
  7.  
  8. void air::init()
  9. {     
  10.      for (int i=0; i<60; i++) { heightIntervals[i] = 250*i; }
  11.      currentDensity = 1.2275;
  12. }
  13.  
  14. double air::getDensity(double height, double heightChange) {
  15.  
  16.      for (int i=0; i<59; i++) {
  17.          if ((height >= heightIntervals[i]) && (height < heightIntervals[i+1])) {
  18.                      currentDensity = currentDensity + kValues[i]*heightChange;
  19.          }
  20.      }
  21.  
  22.      if (height > heightIntervals[59]) { currentDensity = 0; }
  23.  
  24.      return currentDensity;
  25. }
  26.  
Expand|Select|Wrap|Line Numbers
  1. // air.h
  2.  
  3. class air {
  4.  
  5.       double currentDensity;                       
  6.       double heightIntervals[60];
  7.       static double kValues[60];
  8.       public:
  9.            void init();
  10.            double getDensity(double, double);
  11. };
  12.  
  13.  
Nov 18 '06 #5
brixton
35
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?
Nov 18 '06 #6
horace1
1,510 Expert 1GB
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
Nov 18 '06 #7
brixton
35
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.
Nov 19 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: MPowell | last post by:
I'm going through the Koeing book Accelerated C++ in an attempt to understand Container classes. Of course I'm going through a paradigm shift from C to C++. So now I've got struct Header {...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: Dave Smithz | last post by:
Hi There, I have developed a php application for a client that uses the excellent class.phpmailer.php. However, the client has requested that when they get back bounce back emails from people...
2
by: raza | last post by:
In C++ we can do things like (Header is an instance of C++ structure) Send(m_Sock,(char*)&Header,sizeof(Header),0) in C# we have Socket.Send(byte , int offset, int size, flags) how do i...
4
by: Roger | last post by:
I would like to make my column header go verticle instead of horizontal. Is this an easy task in asp.net. (Actually using Visual Studio.net 2003). thanks, Rog
8
by: rodchar | last post by:
hey all, i'm using method webclient.filedownload to automatically download a file from an internet site. that works great. well i was wondering if there was a way to monitor or check if that...
4
by: dsimmons | last post by:
On my website http://seasidequilters.blogspot.com/ I'm having a problem that someone thinks might be associated with Firefox browsers. I'm hoping to find someone in this forum that might be...
0
by: Nol | last post by:
Hi all, My webservice throws an exception, which is translated into a soap Fault in the soap message body. See below for the actual message format as it is send by the server. On the (dotNet)...
3
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th...
4
by: forest demon | last post by:
i've been trying to create a code snippet that would contain a special character...i.e. a trademark symbol (™). but to no avail. whether in the CDATA section or a literal definition, no workie!...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.