#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main ()
{
struct process
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names;
names.push(a[1]);
names.push(a[2]);
cout<<names.front().ar;
return 0;
}
The above code is working fine in eclipse but when i compile it on linux it give many errors. invalid argument and many more. i'm executing it with command:
g++ -o file_name file_name.cpp
does anyone know the reason behind these errors? any solution?
You need to move the definition of struct process out of the main function:
struct process
{
int burst;
int ar;
};
int main ()
{
...
}
Additionally, since you use initializer lists you must enable C++11 suport in GCC:
g++ -std=c++11 -o file_name file_name.cpp
You can not define a struct within a function. In your case, the struct process is defined within the main function.
Also, you need to declare the type of struct you wish to append to the vector.
the '{21, 42}' does not declare a 'process' type (Unless you use C++11).
Using local classes as template arguments and passing braced-init-list to functions are only possible as of C++11. Pass -std=c++11 or -std=c++14 option to g++ and all the errors will disappear. Note that all the letters in the option are in lower case.
Related
The following program fails to compile with g++ -std=c++11 -Wall -Werror test.cpp -o test.o:
#include <thread>
using namespace std;
void fill(int n) {
return;
}
int main() {
thread test(fill, 5);
}
test.cpp:9:12: error: no matching constructor for initialization of 'std::__1::thread'
thread test(fill, 5);
^ ~~~~~~~
Is it because fill is conflicting with std::fill from #include <algorithm>? I haven't included this but I suppose <thread> might've.
Changing my function name to fillie (or anything else pretty much) allows it to compile correctly without linking pthread.
I ask because it is a strange compiler error message, and also it means that the thread constructor can't disambiguate which function I am using based on the parameters (which sort of makes sense, but wanted confirmation).
Yes, the problem is because it is not known whether fill is std::fill or your global fill function.
One way to fix it is to write ::fill to explicitly use the global one.
I'm trying to compile an easy program that use the alias declaration.
This is the code:
#include <iostream>
using namespace std;
using in = int;
in main ()
{
in a = 1;
cout << a << '\n';
return 0;
}
The command I use to compile is g++ -std=c++0x program_name.cxx, using the built-in terminal in Kate on Ubuntu OS.
But it doesn't work! Any suggestion?
(instead using typedef int in; it works).
Compile in C++11 mode. Type aliasing is supported only in C++11. I suspect the g++ version that use is older and doesn't fully support c++11, hence fails with c++0x.
Compile with: g++ -std=c++11 file.cpp
and it works.
By the way, it seems to be a terrible idea to alias int in such a way.
I'm just starting to teach C++, coming from some other languages. I am wishing there were some way to consistently check the API created by a (student) file.
Suppose a student submits this file:
// this is stu.cpp
#include <iostream>
using namespace std;
double x(int y) {return y+0.5;}
Actually, suppose I asked the student to define some other function int x(int). I would like to be able to check this by running this code:
// this is stu.h
int x(int);
// this is gra.cpp
#include "stu.h"
#include <iostream>
using namespace std;
int main() {
cout << x(0); // test their code
}
So I am trying to see if the student's implementation matched the required interface, and testing it on input 0. I would have hoped this would not compile. But when I do
g++ -Wall -Wconversion *.cpp -o gra
./gra
It compiles and runs without crashing, giving output 0. This remains true even if I compile the two files separately and link them after.
I know that nm doesn't list return types. Is that the same reason that we can link together two files when the return values don't match? Is there any sane way to test this? (Like are there compile-time typeof assertions?)
Or is this a specific bug because of int and double being interconvertible? Are there additionall compiler options that could catch this?
Instead of compiling the student's code separately, why don't you just include it directly in your tester program?
int x(int);
#include <stu.cpp>
Then you should get a nice error like this:
a.cpp:2:8: error: functions that differ only in their return type cannot be overloaded
While this is not the "normal" way to compile a student's code, it guarantees that the code can be checked.
Alternatively, you may use a compiler command-line option like -include (GCC, Clang) to force the compiler to include a header file containing your desired API when compiling the student's C++ file. As an example:
api.h
int x(int);
compile with g++ stu.cpp -include api.h, and the appropriate error will be raised.
You can do the following:
// this is gra.cpp
#include "stu.h"
#include "stu.cpp"
#include <iostream>
using namespace std;
int main() {
cout << x(0); // test their code
}
And compile only gra.cpp of course.
I am doing everything correctly as far as I can tell and I have gotten the error message:
error: 'unordered_map' does not name a type
error: 'mymap' does not name a type
In my code, I have:
#include <unordered_map>
using namespace std;
//global variable
unordered_map<string,int> mymap;
mymap.reserve(7000);
void main {
return;
}
I don't see what can be missing here....
EDIT: when I update my declaration to
std::tr1::unordered_map<string,int> mymap;
I an able to eliminate the first error, but when I try to reserve, I still get the second error message.
EDIT2: As pointed out below, reserve must go into main and I need to compile with flag
-std=c++0x
However, there still appear to be errors related to unordered_map, namely:
error: 'class std::tr1::unordered_map<std::basic_string<char>, int>' has no member named 'reserve'
Compile with g++ -std=c++11 (my gcc version is gcc 4.7.2) AND
#include <unordered_map>
#include <string>
using namespace std;
//global variable
unordered_map<string,int> mymap;
int main() {
mymap.reserve(7000); // <-- try putting it here
return 0;
}
If you want to support <unordered_map> for versions older than c++11 use
#include<tr1/unordered_map> and declare your maps in the form :- std::tr1::unordered_map<type1, type2> mymap
which will use the technical report 1 extension for backward compatibility.
You can't execute arbitrary expressions at global scope, so you should put
mymap.reserve(7000);
inside main.
This is also true for other STL containers like map and vector.
My work place is developing code for some absolutely ancient hardware that doesn't have virtual memory. Because of this we're trying to be careful to minimize the text size of object and binary files, so we don't run out of memory. This likely means we'll need to limit the use of templates from the STL, or ban them outright. While looking around to find a way to minimize the size of our files, and still use templates, I came across the -frepo option for g++. A few tests later, I'm even more confused than when I started. The final binary file is the same size or larger when the -frepo is used, which doesn't make sense from me. Can anyone explain to me what this option actually does (other than "it just works," as that's the explanation from the GCC 4.7.1 manual) and how I might be abusing it?
Compiling with g++ -c -frepo main.cpp test8.cpp and linking with g++ test8.o main.o
The .rpo files arebeing created.
test8.h:
#include <list>
using namespace std;
class Test
{
public:
Test ();
list<int> makeNewIntList ();
private:
list<int> intList;
};
test8.cpp:
#include "test8.h"
#include <list>
using namespace std;
Test::Test()
{
intList = list<int>( 10, 12 );
}
list<int> Test::makeNewIntList()
{
intList.push_back(4);
return intList;
}
main.cpp
#include "test8.h"
using namespace std;
void findFive (int num);
list<int> makeIntList();
int main( int argc, char* argv[])
{
Test test;
list<int> intList = test.makeNewIntList();
list<int> intList2 = makeIntList();
list<float> floatList = list<float> (10,12);
floatList.push_back(5);
}
list<int> makeIntList()
{
list<int> intList = list<int> (10,12);
return intList;
}
We are using GCC 4.1.2. Also note GCC 4.7.0 isn't much better, and upgrading the compiler isn't a viable solution.
I would advise that you forget about -frepo, it's a relic and hardly used.
Instead you could look at the extern template syntax for declaring explicit instantiations, to prevent templates being instantiated implicitly, allowing you to control the instantiations so they only happen in one place. To be sure you don't miss any you could also compile with -fno-implicit-templates (which isn't necessary if you're rigorous about using extern template declarations everywhere necessary.)