473,386 Members | 1,864 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,386 software developers and data experts.

Getting a really weird compilation error with g++

26
I have three files: main.cpp, Euler.h, and Euler.cpp. In Euler.cpp, I have a function that calculates prime numbers using Euler's sieve:

Expand|Select|Wrap|Line Numbers
  1. vector<int> sieve(vector<int>& primelist, int i)
  2. {
  3.     bool primes[i];
  4.  
  5.     primes[0] = false;
  6.     primes[1] = false;
  7.  
  8.     for (int j = 2; j < i; j++)
  9.         primes[j] = true;
  10.  
  11.     for (int j = 2; j * j < i; j++)
  12.     {
  13.         if (primes[j])
  14.         {
  15.             for (int k = j; k*j < i; k++)
  16.                 primes[k*j] = false;
  17.         }
  18.     }
  19.  
  20.     for (int k = 2; k < i; k++)
  21.     {
  22.         if (primes[k])
  23.             primelist.push_back(k);
  24.     }
  25. }
  26.  
In Euler.h, I have the forward declaration for the sieve:

Expand|Select|Wrap|Line Numbers
  1. #ifndef EULER_H
  2. #define EULER_H
  3.  
  4. #include <vector>
  5. #include "Euler.cpp"
  6.  
  7. vector<int> sieve(vector<int>& primelist, int i);
  8.  
  9. #endif
  10.  
And then in main.cpp, I have some code for testing my sieve function, through solving of one of Project Euler's problems:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Euler.h"
  3.  
  4. using namespace std;
  5.  
  6. bool isPrime(int check, const vector<int>& primes)
  7. {    
  8.     for (vector<int>::const_iterator it = primes.begin(); *it < check + 1; ++it)
  9.     {
  10.         if (*it == check)
  11.             return true;
  12.     }
  13.  
  14.     return false;
  15. }
  16.  
  17. int main()
  18. {
  19.     vector<int> primes;
  20.     int max_co = 0;
  21.     int max_co_sec = 0;
  22.     int max = 0;
  23.     int x;
  24.  
  25.     sieve(primes, 1000);
  26.  
  27.     for (int a = -999; a < 1000; a++)
  28.     {        
  29.         for (int b = 0; b < 1000; b++)
  30.         {    
  31.             cout << "a " << a << " b " << b << endl;
  32.  
  33.             x = 0;
  34.  
  35.             while (isPrime(x*x+a*x+b, primes))
  36.                 x++;
  37.  
  38.             if (x > max)
  39.             {
  40.                 max = x;
  41.                 max_co = a;
  42.                 max_co_sec = b;
  43.             }
  44.         }
  45.     }
  46.  
  47.     cout << max_co * max_co_sec << endl;
  48.  
  49.     return 0;
  50. }
  51.  
But when trying to compile main.cpp, I am getting the following error:

Expand|Select|Wrap|Line Numbers
  1. In file included from Euler.h:5,
  2.                  from main.cpp:2:
  3. Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token
  4.  
  5.  
What could be causing this?
Dec 22 '10 #1
11 2498
weaknessforcats
9,208 Expert Mod 8TB
#ifndef EULER_H
#define EULER_H

#include <vector>
#include "Euler.cpp"

vector<int> sieve(vector<int>& primelist, int i);

#endif
You do not put code in header files. That is, a header file is for declarations and other things that do not create variables or otherwise generate machine instructions.

So it appears you do not need the Euler.h file. In main.cpp you should include <vector>. Then create the vector variable in main.cpp.

Euler.cpp should compile as a second file of your build. If it needs the vector created in main.cpp, then you can declare an extern in Euler.cpp for that variable.
Dec 22 '10 #2
liams
26
Thanks for the reply, but I'm still getting the same error. Code changed to this:

/**REMOVED EULER.H**/

main.cpp:
Removed the Euler.h include, and instead put:
Expand|Select|Wrap|Line Numbers
  1. #include "Euler.cpp"
  2. #include <vector>
  3.  
In Euler.cpp, I added
Expand|Select|Wrap|Line Numbers
  1. extern
to sieve().

Other than that, everything is the same, but still same error.
Dec 22 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Just an extern won't do it. You need to extern to the function in euler.cpp:

Expand|Select|Wrap|Line Numbers
  1. extern vector<int> sieve(vector<int>& primelist, int i); 
This is a function prototype and you need it in main.cpp.

I got your code to compile after fixing this plus adding a return staement in the sieve function and allocting the primes array on th heap. C++ does not allow a stack array of i elements. That's a g++ add-on.

To use only ANSI C++ you might try using the -pedantic switch.
Dec 22 '10 #4
liams
26
I added a function prototype in main.cpp, just as you said, but now I'm getting:
Expand|Select|Wrap|Line Numbers
  1. Euler.cpp:1: error: expected initializer before ‘<’ token
Also, could you explain what you've done to get my code to compile? I'm not very familiar with the stack and the heap(I heard those terms, and I generally know what they are, but I don't -really- know what they do). Why doesn't C++ allow a stack array of i elements? And, why and how did you allocate the primes on the heap.

p.s
Tried compiling g++ -Wall -pedantic main.cpp, but there's no change in errors/warnings.
Dec 22 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Your function prototype uses vector. Did you #include <vector> before your prototype?

C++ requires that the size of an array to be known at the time the array is created. If your array is a local variable then the compiler needs to know the size at compile time. Hence an array of i elements is not allowed.

Local variables reside in memory in a thing called a stack frame. The compiler needs to know the size of the fram so it generate code to create a frame of the correct size. Local variables are said to be "on the stack" meaning that they are in the stack frame. The life an death of the variables is managed by the compiler so local variablea are aso called "automatic" variables.

If you don't knoe the size of your array you can place a pointer variable on the stack and then allocate memory at run time. Here is where you use the "new" operator. Memory you allocate is said to be "on the heap". It also means you control the life an death of the variable so anything you allocate you are responsible to deallocate using the "delete" operator.
Dec 24 '10 #6
liams
26
How do I do this in my code?
Dec 31 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
My post #4 has the prototype you need in the file with main(). Just copy and paste it into your file.

The linker will match the call in main() with the actual function in euler.cpp.

Just be sure both files are part of the same build. That is, part of the same project,
Jan 1 '11 #8
liams
26
That is exactly what I did but it's not working. Maybe I'm not using correct compiling options. How do I tell g++ to correctly include it in my project?
Jan 1 '11 #9
weaknessforcats
9,208 Expert Mod 8TB
Usually a file is in a project because you put it there.

Have you seen your make file? All of the files to compile and link must be in the make file.

I don't use g++ but I guarantee there is a simple way to add a .cpp file to a project. I use Visual Studio instead. Sorry I can't help furthere
Jan 2 '11 #10
horace1
1,510 Expert 1GB
in the header file you need a using namespace statement
Expand|Select|Wrap|Line Numbers
  1. #ifndef EULER_H
  2. #define EULER_H
  3.  
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<int> sieve(vector<int>& primelist, int i);
  9.  
  10. #endif
  11.  
or specify the scope explicitly
Expand|Select|Wrap|Line Numbers
  1. #ifndef EULER_H
  2. #define EULER_H
  3.  
  4. #include <vector>
  5.  
  6. std::vector<int> sieve(std::vector<int>& primelist, int i);
  7. #endif
  8.  
otherwise compilation fails as vector is an unknown identifer

to build the program I use the command
g++ main.cpp Euler.cpp
Jan 4 '11 #11
weaknessforcats
9,208 Expert Mod 8TB
The "using namespace std" should be inside the functions in a C++ program and not in a header file. Otherwise whenever you include the header you assume namespace std is being used.

Usually, you see the "using namespace std" in each .cpp file and that is better than inside a header file. Ideally, though, the "using namespace std" should be inside each function in C++.
Jan 4 '11 #12

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

Similar topics

2
by: Julia Beresford | last post by:
Hi I am having problems generating classes from an xml schema using XSD.exe. The schema in question is WiX.xsd (a Microsoft schema for WiX - a tool to create MSIs). The xsd tool generates the...
0
by: Nick | last post by:
My HttpModule is only executing on the first run of the application? What would cause this to happen? This is a module that I am using to Authenticate requests instead of a Global.asax. This is...
2
by: James Zhuo | last post by:
Hi all I've been getting the following compilation error. I should explain the background of the project that i am taking over. This is a project that has been developed by someone else a while...
0
by: James Zhuo | last post by:
hi all I changed the name of the class LoginPage to a different name "LoginPageOne" But the same error gets generated with the Wiliam.Request.LoginPageOne. That pretty much leaves me clueless...
2
by: Patrick Huffer | last post by:
When I open a certain page, I receive a "Compilation Error" as follows: Compiler Error Message: CS0117: 'System.Web.UI.HtmlControls.HtmlForm' does not contain a definition for 'ValidateInput'...
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
9
by: subramanian | last post by:
Hello. Consider the following code fragment : enum TestEnum { val1 = 10, val2 = 100, val3 = 1000 }; class Test { public : enum TestEnum { val1 = 1, val2 val3 }; Test(int i = 0, int j = 0,...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
0
by: Jan | last post by:
Hi, when running demo.aspx, i get this error message: Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the...
9
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <vector> using namespace std; template<class Tclass Vec : public vector<T> {
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.