What is the meaning of the address symbol & in a constructor? - c++

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 );

Related

CppCMS build constructor & declaration

From http://cppcms.com/wikipp/en/page/cppcms_1x_forms,
According to this,
A declaration introduces one or more names into a program.....
Therefore, classes, structures, enumerated types, and other
user-defined types can be declared for each compilation unit
AFAIK, constructor should be in myapp.cpp, while declaration should be in content.h. So I put
There are 5 fields on the form. In CppCMS, the form built using 3 codes (the 4th codes are restriction for the fields)
1st codes:
name.message("Your Name");
sex.message("Sex");
marital.message("Marital Status");
age.message("Your Age");
submit.value("Send");
2nd codes:
add(name);
add(sex);
add(marital);
add(age);
add(submit);
3rd codes:
sex.add("Male","male");
sex.add("Female","female");
marital.add("Single","single");
marital.add("Married","married");
marital.add("Divorced","divorced");
4th codes, are restriction for the fields:
name.non_empty();
age.range(0,120);
I'm quite confused which one should be declaration
------------------added
I've tried add all above codes in myapp.cpp like below:
class myapp : public cppcms::application {
public:
myapp(cppcms::service &srv) : cppcms::application(srv)
{
dispatcher().assign("",&myapp::info_form,this);
mapper().assign("");
}
void info_form()
{
name.message("Your Name");
sex.message("Sex");
marital.message("Marital Status");
age.message("Your Age");
submit.value("Send");
add(name);
add(sex);
add(marital);
add(age);
add(submit);
sex.add("Male","male");
sex.add("Female","female");
marital.add("Single","single");
marital.add("Married","married");
marital.add("Divorced","divorced");
name.non_empty();
age.range(0,120);
}
};
But it still giving error:
myapp.cpp: In member function ‘void myapp::info_form()’:
myapp.cpp:20:9: error: ‘name’ was not declared in this scope
myapp.cpp:21:9: error: ‘sex’ was not declared in this scope
myapp.cpp:22:9: error: ‘marital’ was not declared in this scope
myapp.cpp:23:9: error: ‘age’ was not declared in this scope
myapp.cpp:24:9: error: ‘submit’ was not declared in this scope
myapp.cpp:24:9: note: suggested alternative:
/usr/local/include/cppcms/form.h:1574:20: note: ‘cppcms::widgets::submit’
myapp.cpp: At global scope:
myapp.cpp:43:37: error: no ‘void myapp::main(std::string)’ member function declared in class ‘myapp’
my_skin.tmpl:4:2: error: expected unqualified-id before ‘if’
my_skin.tmpl:8:3: error: expected unqualified-id before ‘else’
my_skin.tmpl:12:8: error: expected constructor, destructor, or type conversion before ‘<<’ token
my_skin.tmpl:13:2: error: expected unqualified-id before ‘{’ token

I am having issues with my heading in my function.cpp file

I have one .h which has the class in it and two .cpp, One being the main and the other holding the functions.
My complier is giving me theses errors
Functions.cpp: In member function âvoid Flandre::add()â:
Functions.cpp:10:3: error: âcinâ was not declared in this scope
Functions.cpp:12:33: error: âstrlenâ was not declared in this scope
Functions.cpp:16:6: error: âcoutâ was not declared in this scope
Functions.cpp:16:57: error: âendlâ was not declared in this scope
Functions.cpp:21:7: error: âcoutâ was not declared in this scope
Functions.cpp:21:53: error: âendlâ was not declared in this scope
Functions.cpp:27:9: error: name lookup of âiâ changed for ISO âforâ scoping [-f
Functions.cpp:27:9: note: (if you use â-fpermissiveâ G++ will accept your code)
Functions.cpp:27:16: error: âKYUUâ was not declared in this scope
Functions.cpp:32:6: error: âcoutâ was not declared in this scope
Functions.cpp:32:57: error: âendlâ was not declared in this scope
Functions.cpp:35:17: error: expected primary-expression before â[â token
Functions.cpp:37:14: error: expected unqualified-id before â[â token
Functions.cpp:38:14: error: expected unqualified-id before â[â token
Functions.cpp:39:14: error: expected unqualified-id before â[â token
I think it has something to do with the #include header in Functions
Newprogram2.cpp
>#include <iostream>
#include <string>
#include "newprogram2.h"
Functions.cpp
Some parts are missing but I just want it to complied so I can get add() to work first.
#include "newprogram2.h"
newprogram2.h
#ifndef NEWPROGRAM2_H
#define NEWPROGRAM2_H
#include<string>
using namespace std;
#endif
You have to include the proper headers for the functions you want to use.
For cin, cout and endl you need to #include <iostream>, you forgot to do that in your 2nd .cpp file
The compiler doesn't recognize strlen as a function because it is not in <string> (see http://www.cplusplus.com/reference/string/) but in <string.h> (see http://www.cplusplus.com/reference/cstring/strlen/).
I suggest you use either size() or length(), these are in <string>, both of these can be called on std::string objects.
Functions.cpp:27:16: error: âKYUUâ was not declared in this scope
This error shows because you try to access a variable you declared in another .cpp file. The .cpp file you are trying to access it in doesn't know this variable. You can fix that by moving the variable into the header file.
Functions.cpp:27:9: error: name lookup of âiâ changed for ISO âforâ scoping
This can be fixed by changing this
for(i=0;i<=KYUU;i++)
to this
for(int i=0;i<=KYUU;i++)
Functions.cpp:35:17: error: expected primary-expression before â[â token
Functions.cpp:37:14: error: expected unqualified-id before â[â token
Functions.cpp:38:14: error: expected unqualified-id before â[â token
Functions.cpp:39:14: error: expected unqualified-id before â[â token
These errors show because you try to call functions directly on a class instead of an object instantiated from that class, like this Flandre[i].getid(). You cannot do that, make an object instead and call the functions on the object.

initializer list as argument to operator[]

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.

Why does the compiler tell me vector<string> is not declared?

I have some code here which reads from a file, and stores them in a vector.
I wish to pass this vector to another class. However, when i try to do that, it gives me a strange error, which i do not fully understand. It seems to be saying that the vector is not declared.
Here is the first few lines of a very long error:
g++ C_Main.cpp C_HomePage.cpp C_SelectionPage.cpp -o Project
C_HomePage.cpp:286:40: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:290:26: error: ‘std::vector<std::basic_string<char> > HomePage::getResourcesList’ is not a static member of ‘class HomePage’
C_HomePage.cpp:290:26: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
C_HomePage.cpp:291:2: error: expected primary-expression before ‘return’
C_HomePage.cpp:291:2: error: expected ‘}’ before ‘return’
C_HomePage.cpp:291:2: error: in C++98 ‘HomePage::getResourcesList’ must be initialized by constructor, not by ‘{...}’
C_HomePage.cpp:291:2: error: no matching function for call to ‘std::vector<std::basic_string<char> >::vector(<brace-enclosed initializer list>)’
Here is line 282 - line 292 of C_HomePage.cpp
int HomePage::getInitPoints(){
return initPoints;
}
vector<string> HomePage::getDutiesList(){
return dutiesList;
}
vector<string> HomePage::getResourcesList{
return resourcesList;
}
Here is the corresponding declarations for those methods in H_HomePage.h
class HomePage {
//These values will be the property of the flat
//They are set before the login screen is displayed
string manager;
int initPoints;
vector<string> dutiesList;
vector<string> resourcesList;
vector<FlatMember> flatMemberList;
string loginName;
public:
HomePage(string);
void login(string);
string receivePassword();
void importFlatMembers(string);
void exportFlatMembers(string);
string getLoginName();
string getManager();
int getInitPoints();
vector<string> getDutiesList;
vector<string> getResourcesList;
};
I honestly does not know what is wrong, and have spent many hours getting frustrated over it already. Could someone please help?
You're missing parentheses in the declarations of getDutiesList and getResourcesList:
vector<string> getDutiesList();
vector<string> getResourcesList();
EDIT: You're also missing the parentheses in your .cpp file:
vector<string> HomePage::getResourcesList(){
return resourcesList;
}

MYSQL headers conflict with STL <algorithm> in C++

// File test.cpp
#include <my_global.h>
#include <algorithm>
int main()
{
return 0;
}
Compiled with: g++ -c -I /usr/local/mysql/include/mysql/ test.cpp, where /usr/local/mysql is the mysql install directory.Then the compiler report the following errors:
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/bits/stl_algo.h:61,
from /usr/include/c++/4.4/algorithm:62,
from test.cpp:3:
/usr/include/c++/4.4/bits/algorithmfwd.h:353:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/algorithmfwd.h:364:41: error: macro "min" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: ‘std::min’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:237: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:258: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:259: error: expected declaration before ‘}’ token
I think that there's some name conflict between my_global.h and algorithm, so I wrap my_global.h in a namespace:
// File test.cpp
namespace MYSQL_NAMESPACE {
#include <my_global.h>
}
#include <algorithm>
int main()
{
return 0;
}
But it doesn't help, the compiler still report the same errors. Then I change the include order as following:
// File test.cpp
#include <algorithm>
#include <my_global.h>
int main()
{
return 0;
}
Every thing goes well now.
Does Anybody Really Know What The Problem It Is?
TKS!
It seems that the mysql header defines a macro min.
#if !defined(max)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
This has been reported to MySQL as bug 28184. The bug is marked as closed, so try updating to the newest version. According to the bug page it should be fixed in version 5.1.23, version 6.0.4 and newer versions.
Apparently the namespace trick does not work because min/max are macros and the preprocessor does not look at namespace scope.
This might fix the problem:
#include <my_global.h>
#undef min
#undef max
#include <algorithm>
The whole thing looks horrible though :)
It looks like my_global.h defines some name used by algorithm as a preprocessor macro, causing compilation to fail. With the ordering that works, you won't be able to use whatever it is that my_global.h clobbers, but your code will at least compile unless you need that feature. Since preprocessor macros are not namespaced, the namespace wrapping will not help, as you have observed.
Therefore, it sounds like my_global.h is broken, but if everything works just use the include order that works and go with it. That's my preferred include order anyway - standard library headers first, followed by external library headers, followed by internal headers.