C++ compiling error - c++

I have a compiling error in C++ using classes. I have worked with classes before and have never encountered this error. I have tried adding static before the method ImprtData but that only prompted more errors.
error: invalid use of non-static member function bank.ImprtData;
here is my .cpp
#include "componets.h"
User::User() {
std::cout << "loaded" << std::endl;
}
void User::ImprtData() {
std::cout << "loaded.\n";
}
and here is my .h
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
class User {
public:
User();
void write();
void launch_main_menu();
void login();
void ImprtData();
private:
void deposit();
void withdrawl();
std::string account_name;
int account_pin;
float account_balance;
std::string account_user_name;
};
and finally my main
#include "componets.h"
int main() {
std::cout << "Welcome to Bank 111.\n";
User bank;
bank.ImprtData;
return 0;
}

This is essentially a simple typo. Replace
bank.ImprtData;
with
bank.ImprtData();
to call the function. The expression bank.ImprtData is confusing the compiler since it's interpreting it as the address of a function, and issues a diagnostic since the function is not static.

bank.ImprtData; should be bank.ImprtData();

Related

String declaration problems c++

I was trying to compile this simple program but whenever I try to compile it it gives me many errors and all are string related errors like "syntax error:identifier 'string' " and "undeclared identifier" for my string function and variable.
I tried to delete using namespace std; and using std::string instead but still the same errors occur.
I am using Visual Studio 2017.
#include "Animal.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Animal Cat;
cin.get();
}
and thats the Animal.h:
class Animal
{
public:
Animal();
void SetAnimalName(string x);
string GetName();
void SetAnimalAge(int y);
int GetAnimalAge();
private:
string AnimalName;
int AnimalAge;
};
Animal.cpp
#include "Animal.h"
#include <iostream>
#include <string>
using namespace std;
Animal::Animal()
{
AnimalName = "cat";
AnimalAge = 3;
std::cout << "the Animal is: " << AnimalName << std::endl << "its Age is: " << AnimalAge;
}
void Animal::SetAnimalName(string x) {
AnimalName = x;
}
string Animal::GetName() {
return AnimalName;
}
void Animal:: SetAnimalAge(int y) {
AnimalAge = y;
}
int Animal::GetAnimalAge() {
return AnimalAge;
}
You are missing the #include <string> in your Animal.h which breaks the compilation of your main.cpp.
You are also missing std::string in your Animal.h. As a general rule of thumb, don't use using namespace std and stick with prefixing standard library functions with the std namespace (std::string in your case).

C++ Code::Blocks not compiling with header files

Good evening! (morning?)
I was wondering if anyone is familiar with the following issues. There are three files here, which are Cat.cpp, Cat.h, and CatMain.cpp. The issues are as follows:
When I try to build Cat.cpp, I get the error "undefined reference to WinMain#16".
When I try to build CatMain.cpp, I get undefined reference errors for the speak and jump functions.
The files are in the same folder and the code is just one-liners:
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
Cat.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif // CAT_H
CatMain.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}
Is there anything wrong with this code? Or is anyone aware of whether or not this is a Code::Blocks or a compiler issue?
Any help is greatly appreciated =)
All that your code does as of now is something like this :
Cat.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
CatMain.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
int main()
{
speak();
jump();
return 0;
}
You Cat.cpp has a main method missing because of which it wouldn't compile.
Your CatMain.cpp does not have any definition for speak() and jump(), hence undefined error.
Point : CatMain.cpp doesn't know what Cat.cpp is trying to work out.
int main() { return 0; }
added to Cat.cpp should let it compile.
void speak(){
cout << "defined" << endl;
}
void jump(){
cout << "defined" << endl;
}
added to CatMain.app should work for it as well.
Your code is fine.
Most probably you don't want to compile separate files but project as whole. You should add both cpp to project in your IDE and linker will link them together resolving both issues.

why member declaration is not found in this case

I am using eclipse to write c++ codes. When I try to write a header file and corresponding c++ file, it reminds me "member declaration not found" but I have no idea why this would happen since I declare it in my code...Okay, the codes are like:
LoadMedia.h
#pragma once
#include <string>
#include "LuaBridge.h"
class LoadMedia: public LuaParser
{
public:
LoadMedia();
virtual ~LoadMedia();
void loadScript(luabridge::lua_State* L, const std::string& scriptFilename);
};
And then, the LoadMedia.cpp
#include "LoadMedia.h"
#include "LuaBridge.h"
#include <string>
LoadMedia::LoadMedia(){}
LoadMedia::~LoadMedia(){}
void LoadMedia::loadScript(luabridge::lua_State* L, const std::string& scriptFilename)
{
using namespace luabridge;
if (luaL_dofile(L, scriptFilename.c_str()) != 0)
{
std::cout << "Error, cannot open the script!" << std::endl;
}
}
It reminds me that the loadScript is "member declaration not found"....Any idea why this is so and how should I correct it? Thanks for suggestion.

C2061: syntax error : identifier 'string' - Behaving weird

I am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.

Simple function not working when using namespaces

I have the below code that compiles and executes without error, but the line that should be printed in the menu() function is never printed.
Menu.cpp
#include "stdio.h"
#include "Menu.hpp"
#include <iostream>
using namespace std;
namespace View
{
void Menu::startMenu()
{
cout << "2\n";
}
}
Menu.hpp
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
void startMenu();
};
}
#endif
I wrote a simple test to call the menu function, if it works correctly the output should be
1
2
3
but the 2 is never printed.
MenuTest.cpp
#include "Menu.hpp"
#include "stdio.h"
#include <iostream>
using namespace std;
int main()
{
cout << "1\n";
View::Menu startMenu();
cout << "3\n";
}
Can someone see what's going on here?
View::Menu startMenu();
Declares a function which returns View::Menu type, which is also known as most vexing parse
To initialize an object and call it's member function, you should do:
View::Menu menu;
menu.startMenu();
BTW, you need to make startMenu() function public:
class Menu
{
public: //<-----
void startMenu();
};
See live sample.
help this helps.
Because you declare function "startMenu()", that is returns type "View:Menu"
But you don't call function startMenu().
Try make following code:
View::Menu obj;
obj.startMenu();
PS. And make startMenu() as public:
class Menu
{
public:
void startMenu();
};
When substracting the brckets of View::Menu startMenu();, the code
View::Menu startMenu;
is a object definition of View::Menu, which does not call the function Menu::startMenu(). And that is when "cout << "2\n";" is not executed. To call further Menu::startMenu():
startMenu.startMenu();