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

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

Related

Creating a function that opens a file and reads it using ifstream and ofstream but its QT says i didnt declare the function

Trying to make a function that opens a file using ifstream and offstream but QT compiler tells me my variables are not declared. program is alot longer but its this specific part causing error.
I need to make the function to receive the declared variables in the main code to be able to open the file later on.
Cant Use global variables.
I can make program run without using functions but its a must.
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: variable or field 'OpenUserFile' declared void
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: 'load' was not declared in this scope
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: expected primary-expression before ',' token
void OpenUserFile(load&, ReceiptCreator&,Save&)
^
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: 'ReceiptCreator' was not declared in this scope
void OpenUserFile(load&, ReceiptCreator&,Save&)
^//////////
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: expected primary-expression before ',' token
void OpenUserFile(load&, ReceiptCreator&,Save&)
^////////////
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: 'Save' was not declared in this scope
void OpenUserFile(load&, ReceiptCreator&,Save&)
^/////////
/home/eip/Desktop/atm/ATMFINAL/main.cpp:18: error: expected primary-expression before ')' token
void OpenUserFile(load&, ReceiptCreator&,Save&)
^
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
void OpenUserFile(load&, ReceiptCreator&,Save&){
load.open("id.txt"); // reads id.txt.
if(load.fail()){ //If file has problems, user receives an error message
cout<< "Error reading user id" <<endl;
}
ofstream& ReceiptCreator,Save; // instruction to create a file.
ReceiptCreator.open("receipt.txt", ios::app); //prints out output to external .txt file.
if(ReceiptCreator.fail()){ //if error reading display, error message
cout<< "Error printing transaction receipt" <<endl;
}
}
int main(){
time_t C = time(0); // uses computers curren time
string CT = ctime(&C);// shows user current time
string account, userName, SocialSec, PassNum, UserBalance,Bankname, FirstName;
string LastName, AccountNum, SocialNum;//
int PassFile, PassUser,WithDrawal; // variables for user password
double InitialBalance,Deposit, CurrentBalance;// variables for aritmethic calculations
unsigned ATMNUM = 100+rand()%500; // randomizes atm number
int receipt,process, UI = -99, retry = 1; //
ofstream ReceiptCreator,Save;
ifstream load; // starts process to read user information.
int OpenUserFile(load&, ReceiptCreator&,Save&);
cout << "Loading data from file..." << endl;
void foo(int x){} Is a function with one argument of type int.
void foo(int){} Is a function with one unnamed argument of type int.
void OpenUserFile(load&, ReceiptCreator&,Save&){} Is a function with unnamed arguments of type load&, ReceiptCreateor& and Save&. However those are no types in your code. I'll leave it to you to figure out the correct signature.
You have to fix the errors one by one, because one error can confuse the compiler and trigger other errors. One more problem I can spot now is
ofstream& ReceiptCreator,Save;
References cannot be not initialized. I suppose this line was just a trial to fix the code, as the names are those of the arguments, then you can remove that line. If not, you need to decide whether to pass the streams as arguments or have them only inside the function, not both.

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

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

Typedef not a member of namespace

So, I'm attempting to fork some open source code and upon compilation I am greeted with these errors:
C2039 'TransactionId': is not a member of 'CryptoNote'
C2061 syntax error: identifier 'TransactionId'
I'm relatively inexperienced with C++ usually confining myself to the realms of C#, however, I can clearly see that TransactionId is a typedef declared in a different file like so:
namespace CryptoNote {
typedef size_t TransactionId;
typedef size_t TransferId;
//more code
And the line throwing the error is:
void sendTransactionCompleted(CryptoNote::TransactionId _id, bool _error, const QString& _error_text);
To my inexperienced eyes, that looks as though TransactionID is definitly a member of Cryptonote is it not?
Any ideas what's going on?
The repo is here: https://github.com/hughesjs/Incendium_GUI
And the necessary submodule is here: https://github.com/hughesjs/Incendium_Crypt
Those typedefs are defined in Incendium_Crypt/include/IWalletLegacy.h.
void sendTransactionCompleted(CryptoNote::TransactionId _id, bool _error, const QString& _error_text);`
is defined in Incendium_GUI/src/gui/SendFrame.h, which includes IWallet.h. However, IWallet.h does not in turn include IWalletLegacy.h. Hence, those typedefs are unknown to SendFrame.h.
It's difficult to say without seeing all the code but a few things come to mind:
Firstly is this the first error you get. Compilation errors with C++ tend to result in a bunch of secondary errors. For example the following results in a similar error to what you see but fails to compile because size_t has not been defined:
namespace CryptoNote {
typedef size_t TransactionId;
typedef size_t TransferId;
}
int main(void)
{
CryptoNote::TransactionId id;
return 0;
}
$ g++ -std=c++11 namespace.cxx -o namespace
namespace.cxx:4:9: error: ‘size_t’ does not name a type
typedef size_t TransactionId;
^~~~~~
namespace.cxx:5:9: error: ‘size_t’ does not name a type
typedef size_t TransferId;
^~~~~~
namespace.cxx: In function ‘int main()’:
namespace.cxx:11:17: error: ‘TransactionId’ is not a member of ‘CryptoNote’
CryptoNote::TransactionId id;
^~~~~~~~~~~~~
See http://www.cplusplus.com/reference/cstring/size_t/ for a list of headers that define size_t.
Is CryptoNote nested inside another namespace?
Is there another CryptoNote defined in the namespace your function is declared in?
Are these in the same header file? If not, is the header file where the namespace is defined included in the header file containing the function declaration?

Nested Class. error: expected parameter declarator - for inner class instance

I started learning about nested classes in C++, I tried a quick code which I pasted here to see how nested classes work. But the compilation end with some errors which I can't figure out.
File: check.cpp
class Outside{
public:
class Inside{
private:
int mInside;
public:
Inside(const int& x):mInside(x){}
};
private:
Inside mOutside(20);
};
int main(void){
Outside o;
return 0;
}
The error which I get on compiling by g++ -Wall -std=c++11 -o check.out check.cpp
check.cpp:12:25: error: expected parameter declarator
Inside mOutside(20);
^
check.cpp:12:25: error: expected ')'
check.cpp:12:24: note: to match this '('
Inside mOutside(20);
^
I need a good explanation behind this error and how to overcome this error.
You have to use = or {} for in-place member initialization:
// ...
private:
Inside mOutside = 20;
The parentheses form would be ambiguous (it could be confused with a function declaration).
Inside mOutside{20};
With clang++ this triggers the warning:
warning: private field 'mInside' is not used [-Wunused-private-field]
and the compiler has a point. The strange thing is the missing warning with the other form (=).
Try using this way of member initializing.
Inside mOutside = Inside(20);
Yes, your solution worked thank you. But how? Why?
See Initializing bases and members in open-std.

Unable to pass reference from one class to another

I'm writing a C++ program intended to manage the resources and duties in a flat.
This program is intended to run in Linux Shell.
I created a class call HomePage for users to login. I've also got another class called SelectionPage. Which are the Menus after the user got past the login. In my main functions, i wish to pass some data obtained by an instance of the HomePage class, to an instance of the SelectionPage class.
I've been stuck here for at least 6 hours, Could someone please help?
Here's the error message:
g++ C_Main.cpp C_HomePage.cpp C_SelectionPage.cpp -o Project
C_Main.cpp: In function ‘int main()’:
C_Main.cpp:17:93: error: no match for call to ‘(std::vector<std::basic_string<char> >) ()’
C_Main.cpp:17:122: error: no match for call to ‘(std::vector<std::basic_string<char> >) ()’
C_HomePage.cpp:290:41: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:294:44: error: no ‘std::vector<std::basic_string<char> > HomePage::getResourcesList()’ member function declared in class ‘HomePage’
C_SelectionPage.cpp:9:144: error: declaration of ‘SelectionPage::SelectionPage(std::string, int, std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >, std::string)’ outside of class is not definition
C_SelectionPage.cpp:9:146: error: expected unqualified-id before ‘{’ token
make: *** [Project] Error 1
Here is my main()
// Testing Home Page Functionality
#include "H_HomePage.h"
#include "H_SelectionPage.h"
using namespace std;
string initializationFile = "D_initialization.dat";
string flatMemberFile = "D_flatMember.dat";
int main()
{
HomePage frontEnd(initializationFile); //Create Boundary Object
system("clear"); //Clear Terminal Screen
frontEnd.login(flatMemberFile);
SelectionPage Menu(frontEnd.getManager(), frontEnd.getInitPoints(), frontEnd.getDutiesList(), frontEnd.getResourcesList(), frontEnd.getLoginName());
Menu.showManagerMenu();
return 0;
}
Here's my constructor for SelectionPage class, as described in the header file:
...
SelectionPage(string Manager, int Points, vector <string> dutiesList, vector <string> resourceList, string loginName);
...
Here's the implementation of the constructor for SelectionPage class:
SelectionPage::SelectionPage(string newanager, int points, vector <string> newDutiesList, vector <string> newResourceList, string newLoginName);{
manager = newManage;
initPoints = points;
dutiesList = newDutiesList;
resourceList = newResourceList;
loginName = newLoginName;
}
could someone please, please help? I'll be ever so grateful!
Look at your error messages carefully.
C_HomePage.cpp:290:41: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:294:44: error: no ‘std::vector<std::basic_string<char> > HomePage::getResourcesList()’ member function declared in class ‘HomePage’
You probably have incorrect declarations for HomePage::getDutiesList() and HomePage::getResourcesList(), or none at all. It's hard to tell what exactly is wrong with them without looking at the class declaration. This is also the reason of compiler errors in your main().
The reason of the other two compiler errors is probably the spurious ; between ) and {' in the definition ofSelectionPage::SelectionPage`. Get rid of it.