'OBJECT' not declared in scope - c++

My first ever c++ program and i cant seem to figure out why it says that my 'object' was not declared in the scope. I've included the header files, i've prototyped my functions. i'm used to coding in java and the separation is pretty confusing to me.
The aim of the program is simple: create a class: Semester.
Semester: will have ID, name, credit, marks.
provide get/set methods to input and output from kb
Main
#include <iostream>
#include <stdio.h>
#include "Semester.h"
using namespace std;
int main()
{
Semester s1;
cout << "Please enter ID" << endl;
cin >> sl.setid();
cout << "ID: " << s1.getid();
}
cpp
#include <stdio.h>
#include <iostream>
#include "Semester.h"
using namespace std;
Semester::Semester(){
}
//setters
void Semester::setid(string t_id) {id = t_id;}
void Semester::setunit(string t_name) {unit_name = t_name;}
void Semester::setcredit(int t_credit){credit = t_credit;}
void Semester::setmark(int t_marks) {marks = t_marks;}
//getters
string Semester::getid() {return id;}
string Semester::getunit() {return unit_name;}
int Semester::getcredit() {return credit;}
int Semester::getmark() {return marks;}
header
#ifndef SEMESTER_H
#define SEMESTER_H
#include <stdio.h>
#include <iostream>
class Semester{
private: //variables
std::string id;
std:: string unit_name;
int credit, marks;
public:
Semester(); //constructor
//setters
void setid(std::string id);
void setunit(std::string name);
void setcredit(int credit);
void setmark(int marks);
//getters
std::string getid();
std::string getunit();
int getcredit();
int getmark();
};
#endif // SEMESTER_H

The following line is incorrect.
cin >> sl.setid();
Problems with that line:
You are calling a function that expects an argument with no arguments.
The return type of setid() is void. That cannot be used as the RHS of the stream extraction function.
What you need is:
std::string id;
cin >> id;
s1.setid(id);
Suggestion for improvement: Make all the get functions const member functions.
//getters
std::string getid() const;
std::string getunit() const;
int getcredit() const;
int getmark() const;
Make sure to update the .cpp file accordingly.

Related

Why isnt my .cpp class using my .h variables?

So I am using Visual Studio to make a simple program for intro to Object Oriented Programming. I am using C++ language to do this, in Netbeans with JAVA OPP isn't so complicated but I am having trouble here. I have to make a simple Object, I chose to make my object called Movie. I made a Movie.h and Movie.cpp file. I included .h's extensions to my Movie.cpp and main.cpp but when I create the object in my main or try to i keep getting errors and underlines because my .cpp file is not recognizing my variables declared in .h, it keeps saying the variable is undefined.
So visual studio's wants to help me out and I followed their method so in my .h files they said they will define my methods for me, meaning they will set it up for me and when I clicked it i get this format
string Movie::getName()
{
return string();
}
While I am using this format for this function
string getName(){
return name;
}
My variables keeps getting red underlined saying they are undefined.
My Movie.h file
#pragma once
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z );
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
My Movie.cpp File
#include "Movie.h"
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void setLength(int x) {
length = x;
}
void setRating(double y) {
rating = y;
}
void setName(string z) {
name = z;
}
//Get functions
int getLength() {
return length;
}
double getRating() {
return rating;
}
string getName() {
return name;
}
My .main
#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1();
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Now I havent worked on my toString method yet but I can't because I cant figure out what the real problem is, why is my .cpp not recognizing my variables? Is my format wrong? Is the visual studio's format right because I ran with their format and got bunch of errors as well or am I declaring them wrong or something? Thank you!
Main.cpp
//#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1;
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Movie.h
#pragma once
#ifndef Movie_H
#define Movie_H
#include <string>
#include <iostream>
using namespace std;
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z);
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
#endif
Movie.cpp
#include "Movie.h"
//#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void Movie::setLength(int x) {
length = x;
}
void Movie::setRating(double y) {
rating = y;
}
void Movie::setName(string z) {
name = z;
}
//Get functions
int Movie::getLength() {
return length;
}
double Movie::getRating() {
return rating;
}
string Movie::getName() {
return name;
}

ERROR using a class as a vector type - "Undeclared identifier"

Hello Stack Overflow!
I have encountered a problem which I have tried to solve but have not succeeded with, therefore I am turning my head towards you programmers hoping for answers.
What I am suspecting the problem is, is that it has to do with my main.cpp which I haven't started coding in yet for reasons. Could be very wrong though.
Any tips/hints are appreciated!
The error is about this part located in the BankFunctions header
// Vector
static std::vector<accounts> users;
The error messages are listed at the bottom.
I have included the two class header files.
Main.cpp:
// Classes
#include "BankFunctions.h" // Handles the bank functions
#include "accounts.h" // Handles the customer accounts
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
int main() {
return 0;
}
Class 1: Accounts
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "BankFunctions.h"
class accounts
{
public:
// Constructor
accounts(
unsigned int newId,
unsigned int newAge,
unsigned int newSSN,
std::string newFirstName,
std::string newLastName,
std::string newAdress,
std::string newEmail,
double newBalance
);
// Overload constructor
accounts(std::string eId, std::string eNAge, std::string eSSN, std::string eFName, std::string eLName, std::string eEmail, std::string eAdress, std::string eNBalance);
// Mutators
inline void setId(unsigned int i) { id = i; }
inline void setAge(unsigned int a) { age = a; }
inline void setSSN(unsigned int ssn) { SSN = ssn; }
inline void setFirstName(std::string FN) { firstName = FN; }
inline void setLastName(std::string LN) { lastName = LN; }
inline void setEmail(std::string em) { email = em; }
inline void setAdress(std::string adr) { adress = adr; }
inline void setBalance(double newBalance, bool t);
// Accessors
inline unsigned int getId() const { return id; }
inline unsigned int getAge() const { return age; }
inline unsigned int getSSN() const { return SSN; }
inline std::string getFirstName() const { return firstName; }
inline std::string getLastName() const { return lastName; }
inline std::string getEmail() const { return email; }
inline std::string getAdress() const { return adress; }
inline double getBalance() const { return balance; }
private:
// Customer account details
unsigned int id;
unsigned int age;
unsigned int SSN; // Social Security Number
std::string firstName;
std::string lastName;
std::string adress;
std::string email;
double balance;
};
Class 2: BankFunctions
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "accounts.h"
class BankFunctions
{
public:
BankFunctions();
static void loadVector(); // Loads in customer account information into objects that get stored into a vector. (All the existing accounts get loaded in)
static void newCustomer(); // Create new customer account
static void existingCustomer(); // View customer account
static void deposit(unsigned int accId); // Deposit money function
static void withdraw(unsigned int accId); // Withdraw money function
// Edit customer account
static void editCustomerDetails(unsigned int accId);
// Related Functions
static void editAge(unsigned int accId);
static void editSSN(unsigned int accId);
static void editFirstName(unsigned int accId);
static void editLastName(unsigned int accId);
static void editAdress(unsigned int accId);
static void editEmail(unsigned int accId);
static void editBalance(unsigned int accId);
private:
// Vector
static std::vector<accounts> users;
static unsigned int amountOfAccounts;
};
ERROR Message:
...bankfunctions.h(36): error C2065: 'accounts': undeclared identifier
...bankfunctions.h(36): error C2923: 'std::vector': 'accounts' is not a valid template type argument for parameter '_Ty'
...bankfunctions.h(36): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
Try just to remove the include BankFunctions.h from the accounts.h.
The problem, I think, is that an include performs simply an inserting of the whole header file at the position where the include is done, but pragma once ensures that you do not include a header twice into a cpp file.
When your headers include each other it is possible to get the class definition of accounts past the line 36 of BankFunctions.h where you need it.
You may also put an extra class accounts; on the very top of main.cpp. This should also work, but is not a pretty solution.

No variable member function declared in class?

can sombody explain to me why my code will not work, and how to fix it thanks :)
I keep recieving this error :
no 'int burrito::setName()' member function declared in class 'burrito'
My goal is to call a function from a different class file
My main.cpp :
#include <iostream>
#include "burrito.h"
using namespace std;
int main()
{
burrito a;
a.setName("Ammar T.");
return 0;
}
My class header (burrito.h)
#ifndef BURRITO_H
#define BURRITO_H
class burrito
{
public:
burrito();
};
#endif // BURRITO_H
My class file (burrito.cpp):
#include "burrito.h"
#include <iostream>
using namespace std;
burrito::setName()
{
public:
void setName(string x){
name = x;
};
burrito::getName(){
string getName(){
return name;
};
}
burrito::variables(string name){
string name;
};
private:
string name;
};
Your code is a mess. You need to write function prototypes in the header file and function definitions in the cpp file. You are missing some basic coding structures. See below and learn this pattern of coding:
This code should work and enjoy burritos !
main():
#include <iostream>
#include "Header.h"
int main()
{
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
getchar();
return 0;
}
CPP file:
#include "Header.h"
#include <string>
void burrito::setName(std::string x) { this->name = x; }
std::string burrito::getName() { return this->name; }
Header file:
#include <string>
class burrito
{
private:
std::string name;
public:
void setName(std::string);
std::string getName();
//variables(string name) {string name;} // What do you mean by this??
};
Your poor little burrito is confused. Confused burritos can't help much.
You may want your burrito declaration as:
class Burrito
{
public:
Burrito();
void set_name(const std::string& new_name);
std::string get_name() const;
private:
std::string name;
};
The methods could be defined in the source file as:
void
Burrito::set_name(const std::string& new_name)
{
name = new_name;
}
std::string
Burrito::get_name() const
{
return name;
}
The header file only has a constructor for the class. The member functions
setName(string) and getName()
are not declared in the header file and that is why you get the error.
Also, you need to specify the return type for functions.
One way to do this would be
//Header
//burrito.h
class burrito{
private:
string burrito_name;
public:
burrito();
string getName();
void setName(string);
}
//burrito.cpp
#include "burrito.h"
#include <iostream>
using namespace std;
string burrito::getName()
{
return burrito_name;
}
void burrito::setName(string bname)
{
bname =burrito_name;
}
This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:
#include <iostream>
#include <string>
using namespace std;
class burrito {
public:
void setName(string s);
string getName();
private:
string name;
};
void burrito::setName(string s) {
name = s;
}
string burrito::getName() {
return name;
}
int main() {
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
return 0;
}

How to do you call a function inside separated class?

So, I tried to make this code :
#include <iostream>
using namespace std;
class BuckysClass{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
BECOMING TO A SEPARATED CLASS like this :
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
int main (){
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
==============
#ifndef BUCKYSCLASS_H
#define BUCKYSCLASS_H
class BuckysClass
{
public:
void setName(string x);
string getName();
private:
string name;
};
#endif // BUCKYSCLASS_H
=============
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
BuckysClass::BuckysClass()
{
}
void setName(string x){
name = x;
}
string getName(){
return name;
}
When I run the first code, I succeed,
but I got error when running the separated class code,
help me find out what's wrong ???
I tried to use different code,
but it seems I can't find the reason,
the closest reason I believe, is the main doesnt call the function on the separated class properly.
If you write this code:
void setName(string x){
name = x;
}
string getName(){
return name;
}
The compiler interprete it as two functions called setName and getName, it has no idea they are member functions of your BuckysClass class.
You have to precise it with the following syntax:
void BuckysClass::setName(string x){
name = x;
}
string BuckysClass::getName(){
return name;
}
Additionally, here you are defining a default constructor:
BuckysClass::BuckysClass()
{
}
But you didn't put it in the class prototype. You have to add it somewhere in the class prototype definition in your .h file, or your compiler won't recognize it:
class BuckysClass
{
public:
BuckysClass(); // Default constructor.
void setName(string x);
string getName();
private:
string name;
};

I dont understand whats wrong mith my code with multiple classes (I started c++ multiple classes recently)

I recently started learning and working on multiple classes but i can't understand why my code doesn't work.
I get these errors:
errors photo
This is my code separated in these classes
main.cpp:
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
int main(){
string input;
dalykai dalykaiObj;
cout << "Type some name: "; cin >> input;
dalykaiObj.setName(input);
cout << "Jusu ivestas vardas yra: " << dalykaiObj.getName() << endl;
return 0;
}
dalykai.h
#ifndef DALYKAI_H
#define DALYKAI_H
using namespace std;
class dalykai{
public:
dalykai();
void setName(string x);
string getName();
protected:
private:
string name;
};
#endif // DALYKAI_H
dalykai.cpp
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
string name;
dalykai::dalykai(){
cout << "Object was created successfully!\n";
}
dalykai::void setName(string x){
name = x;
}
dalykai::string getName(){
return name;
}
This is wrong:
dalykai::void setName(string x){
name = x;
}
It should be:
void dalykai::setName(string x){
name = x;
}
In addition. Avoid using using namespace std in headers. Your get function should end with const since it doesn't change class member variables.