This is the program I'm trying to compile.
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
using namespace std;
vector<string> paramlist;
const char *programname = "abc";
const char **args = new const char* [paramlist.size()+2]; // extra room for program name and sentinel
args [0] = programname; // by convention, args[0] is program name
for (int j = 0; j < paramlist.size()+1; ++j) // copy args
args [j+1] = paramlist[j] .c_str();
args [paramlist.size()+1] = NULL; // end of arguments sentinel is NULL
execv (programname, (char **)args);
When I try to compile it I get these error messages:
test.cpp:11: error: expected constructor, destructor, or type conversion before ‘=’ token
test.cpp:12: error: expected unqualified-id before ‘for’
test.cpp:12: error: expected constructor, destructor, or type conversion before ‘<’ token
test.cpp:12: error: expected unqualified-id before ‘++’ token
test.cpp:15: error: array bound is not an integer constant
test.cpp:15: error: expected constructor, destructor, or type conversion before ‘=’ token
test.cpp:17: error: expected constructor, destructor, or type conversion before ‘(’ token
You at least need an int main function.
Your program contains code but it needs to be contained within a function. Try wrapping all your code after the using namespace std; line with a function like int main. Google for any "Hello world" C++ sample to see an example.
Related
I'm looking up c++ library, and see the istream class, I am confused with a contractor with an address symbol. what is the meaning of a constructor with an address symbol?
one of the istream constructors is.
protected: iostream& (iostream&& x);
I found it in website cplusplus.com,
link: iostream
I defined a customer class with a similar constructor that has a & symbol:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
I use the below command to compile it:
g++ Test.cpp -o Test -std=c++11
however, I get some compile error messages:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
I got confused, c++ library istream class is fine. why did my custom class constructor fail? what am I missing?
The information on cplusplus.com is... sometimes not dependable. (See What's wrong with cplusplus.com? for a discussion of this.) On CPPReference, you can see that the move constructor is, you know, just a regular move constructor.
This is a bug in http://www.cplusplus.com/reference/istream/iostream/iostream/.
If you look at https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream, you will find
protected: basic_iostream( basic_iostream&& other );
I am trying to launch a function threaded using launch::async. However, I noticed that this doesn't work when passing struct elements as parameter:
The code
#include <future>
#include <vector>
#include <thread>
struct example { int ten; };
void threadFunction(int number, std::string hi) {
printf("%s Your number is %d\n", hi.c_str(), number + 1);
}
int main() {
example ex;
ex.ten = 9;
std::string someString = "Hi there!";
std::vector< std::future< void > > threads(5);
for (uint16_t s = 0; s < 5; s += 1) {
threads[s] = async(std::launch::async,
[ex.ten,
someString] {
threadFunction(ex.ten, someString);
});
}
}
gives the following errors:
file.cpp: In function ‘int main()’:
file.cpp:25:39: error: expected ‘,’ before ‘.’ token
[ex.ten,
^
file.cpp:25:39: error: expected identifier before ‘.’ token
file.cpp:25:43: error: expected ‘]’ before ‘,’ token
[ex.ten,
^
file.cpp: In lambda function:
file.cpp:25:43: error: expected ‘{’ before ‘,’ token
file.cpp: In function ‘int main()’:
file.cpp:26:46: error: expected ‘)’ before ‘]’ token
someString] {
^
file.cpp:28:8: error: expected primary-expression before ‘)’ token
});
When replacing ex.ten with some other variable ten it does work.
So my questions are:
1. Why does launch::async not work with struct elements?
2. Is there a way to do it in a more elegant way than to make a variable for each element in the struct and pass those variables instead? (such as int ten = ex.ten; etc)
You can't pass single struct field that way into lambda in C++. The error you get is not connected to std::launch or communicating between threads. What you can do instead is:
C++11 - copy field into local variable and capture that variable:
auto ten = ex.ten;
threads[s] = async(std::launch::async,
[ten,
someString] {
threadFunction(ten, someString);})
C++14 and later - initialize variable in capture list:
threads[s] = async(std::launch::async,
[ten = ex.ten,
someString] {threadFunction(ten, someString);})
What you are trying to do is to capture a field in the struct in lambda. This is not possible, you can only capture the whole struct.
It has nothing to do with std::launch, it's a basic property of lambda closure.
In C++14, you can have captures with initializers, which might be what you want:
auto lam = [ten = ex.ten]...
I am getting this error during compile time (g++ 4.4.6):
main.cpp: In function ‘int main()’:
main.cpp:27: error: expected initializer before ‘:’ token
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘;’ before ‘for’
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘)’ before ‘for’
main.cpp:33: error: expected initializer before ‘:’ token
main.cpp:36: error: could not convert ‘((list != 0u) ? (list->SortedList::~SortedList(), operator delete(((void*)list))) : 0)’ to ‘bool’
main.cpp:37: error: expected primary-expression before ‘return’
main.cpp:37: error: expected ‘)’ before ‘return’
My code is as follows:
#include <iostream>
#include "Student.h"
#include "SortedList.h"
using namespace std;
int main() {
SortedList *list = new SortedList();
Student create[100];
int num = 100000;
for (Student &x : create) { // <--Line 27
x = new Student(num);
num += 10;
}
for (Student &x : create)
list->insert(&x);
delete list;
return 0;
}
Anybody who possibly knows the source of the error would be of great help. Also, Student and SortedList are objects which are declared in their .h files.
According to this page on GCC's website, range-based for is only available in g++ 4.6 and up, so you'll have to convert your code to a normal for loop or use std::for_each or something, or upgrade your compiler.
This question is related to the one discussed here.
I try to use an initializer list to create an argument to be passed to operator[].
#include <string>
#include <vector>
struct A {
std::string& operator[](std::vector<std::string> vec)
{
return vec.front();
}
};
int main()
{
// ok
std::vector<std::string> vec {"hello", "world", "test"};
A a;
// error: could not convert '{"hello", "world", "test"}' to 'std::vector...'
a[ {"hello", "world", "test"} ];
}
My Compiler (GCC 4.6.1) complains:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20:8: error: expected primary-expression before ‘{’ token
test.cpp:20:8: error: expected ‘]’ before ‘{’ token
test.cpp:20:8: error: expected ‘;’ before ‘{’ token
test.cpp:20:35: error: expected primary-expression before ‘]’ token
test.cpp:20:35: error: expected ‘;’ before ‘]’ token
Should this be valid C++11?
Interestingly, when using operator() instead of operator[] it works.
Yes, it is valid C++11 and should work in any compliant compiler.
Please note that C++11 support in gcc is quite immature, and this code example will not compile in any 4.6 version, but only in 4.7 svn snapshot versions.
I'm trying to convert wstring to string with use of locale facets but I got stuck on following error:
test_facet.cpp: In function ‘int main()’:
test_facet.cpp:14: error: invalid initialization of reference of type ‘std::ctype<wchar_t>&’ from expression of type ‘const std::ctype<wchar_t>’
/usr/include/c++/4.4/bits/locale_facets.h:1430: error: ‘virtual char std::ctype<wchar_t>::do_narrow(wchar_t, char) const’ is protected
test_facet.cpp:16: error: within this context
Source:
#include <iostream>
#include <string>
#include <locale>
#include <algorithm>
using namespace std;
int main()
{
locale loc("");
std::wstring Str = L"ěščřžýáíé";
std::string Str2;
ctype<wchar_t> &ct = std::use_facet<std::ctype<wchar_t> >(loc);
for(std::wstring::const_iterator It = Str.begin(); It < Str.end(); ++It)
ct.do_narrow(*It, 'X' );
std::cout << Str2 <<std::endl;
}
Could someone tell me, what I am dooing wrong?
Thanks
2 things:
1) use_facet returns reference to const, so you can't assign it to a non-const one. So declare ct as:
const ctype<wchar_t> &ct = ....
2) As the second error message states, do_narrow is protected, making it unaccessible to external callers. Use narrowinstead, which is public.
You cannot invoke do_narrow from this context. Only member methods of class ctype (and deriveds) are allowed to call do_narrow.