Cant call functions when placing class in seperate file [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So Im new to C++, im trying to create a function to calculate the area of a triangle when the user inputs the base and height, however whenever i try to build and run this program I get an error saying: ISO C++ forbids declaration of "calcArea" with no type [-fpermissive]
Area.h
#ifndef AREA_H
#define AREA_H
#include <iostream>
using namespace std;
class Area
{
private:
int base;
int height;
public:
Area();
int calcArea();
};
#endif // AREA_H
Area.cpp
#include "Area.h"
#include <iostream>
using namespace std;
Area::Area()
{
cin >> base;
cin >> height;
};
Area::calcArea(){
int answer;
answer = base * height;
return answer;
}

You are missing the return type.
int Area::calcArea(){
//^^^

Your current
Area::calcArea(){
int answer;
...
definition misses to specify a return type matching the declaration int calcArea(); from your Area class declaration.
As T.C. already showed it needs to be
int Area::calcArea(){
//^^^
int answer;
...

Related

C++ use of undeclared identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I know there's a lot of different threads with this similar issue but I've gone through a number of them and have fixed any of the possible issues that may be resulting in this error but I keep getting it.
I'm trying to test some of functions in my class file but whenever I try calling them I get that error.
Here's one for example.
main.cpp
#include <iostream>
#include <vector>
#include "Graph.h"
using namespace std;
int main(){
Graph graph;
newState();
return 0;
}
Graph.h (I know I shouldn't be using namespace here but I'm still doing it for now)
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class Graph{
public:
Graph();
Graph(vector<Edge> const &edges, int N);
void printGraph(Graph const &graph, int N);
// print adjacency list representation of graph
void createTree(GraphNode *root, int state[]);
void newState();
private:
int states = 8;
int initialState[8];
int nextState[8];
};
Graph.cpp
#include "Graph.h"
void Graph::newState(){
srand (time(NULL));
int random = rand() % 4 + 1;
cout << random;
};
I feel like I'm trying to do the bare minimum but it just does not want to work. Can anyone please tell me where I'm going wrong?
newState is not a free-standing function. It is a non-static member function of Graph class. Which means, it must be called on an instance of Graph. What you probably meant to do is:
Graph graph;
graph.newState();

beginner in c++ and i face this problem error C3867 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
this my first c++ project by using classes and i face this problem
error C3867
my main code is
#include <string>
#include <iostream>
#include "Car.h"
int main() {
Car c1;
c1.setMaker("Honda");
c1.setModel(2018);
cout << "This Car Made By" << c1.getMaker << "\n";
cout << "This Car Model" << c1.getModel << "\n";
}
the header is
#pragma once
#include <string>
using namespace std;
class Car {
private:
string maker;
int model;
public:
void setMaker(string m);
string getMaker();
void setModel(int m);
int getModel();
the cpp is
#include "Car.h"
void Car::setMaker(string l) { maker = l; }
string Car::getMaker() { return maker; }
void Car::setModel(int m) { model = m; }
int Car::getModel() { return model; }
and this is the error message:
error C3867: 'Car::getMaker': non-standard syntax; use '&' to create a pointer to member
i've tried everything i know as a beginner but i can't make it work :(
You need to call functions with a parameter list at the end, even if that list is empty.
E.g
c1.getMaker is the address to the getMaker function
c1.getMaker() actually calls the function

C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a ran in to this error (error C3867: non-standard syntax; use '&' to create a pointer to member) a couple of times. I know this question has been asked a lot of times, but I don't get why the problem happens and what I can do to fix it. I've read a lot of guides how pointers work and I've tried to play with the new knowledge, but I don't know how to do it correctly.
For this question I have made a simple code. Can someone help me understand why this error occurs and how to fix this code?
Error: error C3867: 'BankAccount::amountOfMoney': non-standard syntax; use '&' to create a pointer to member
Source.cpp
#include <iostream>
#include <string>
#include "BankAccount.h"
using namespace std;
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney << endl;
}
BankAccount.h
#pragma once
#include <string>
using namespace std;
class BankAccount
{
public:
BankAccount();
BankAccount(string name, double money);
~BankAccount();
double amountOfMoney();
private:
string name;
double money;
};
BankAccount.cpp
#include "BankAccount.h"
BankAccount::BankAccount()
{
}
BankAccount::BankAccount(string n, double m) {
name = n;
}
BankAccount::~BankAccount()
{
}
double BankAccount::amountOfMoney() {
return money;
}
You forgot the function call operator (). Change your main code to:
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney() << endl;
}
Without the parentheses it tries to print the address of a member function, which it is not able to do unless the function is not a member of a class.
If you want to call your member function, use brackets.:
cout << bankAccount1.amountOfMoney() << endl;

Having trouble with the error: expected unqualfied-id before '{' just a header for a class that I need just not sure what is causing the error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
{
class book
{
public:
set_book();
show_book();
private:
string title,author;
int pages,date;
};
}
In addition to the points made by other answers...
You have:
set_book();
show_book();
These are not valid member function declarations. They need, at the least, a return type.
void set_book();
void show_book();
Please add any input arguments needed by them.
You are missing includes (string), you should avoid using namespace std in a header.
And also, remove this extra scope :
using namespace std;
// { <------
class book
{
public:
set_book();
show_book();
private:
string title,author;
int pages,date;
};
// } <------
You have a bogus set of { and }.
Change to:
#include <iostream>
using namespace std; // this is a bad idea BTW
class book
{
// etc.
Also it'd be a good idea to #include <string> in case whoever includes your file does not include that; and if you don't actually use iostream then don't include it.

Find my bug. C++ programming exercise [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#ifndef _SOLDIER_H_
#define _SOLDIER_H_
#include <iostream>
#include <stack>
using namespace std;
class Soldier {
// your code here
private:
stack<int> list;
public:
Soldier(int num);
bool check(vector<int> arrange);
};
#endif
Error is at the above Soldier.h, at bool check(vector arrange);
15 C:\Users\king boon\Desktop\CS1020E\lab4\lab4\ex1\skeleton\Soldier.h expected `;' before '(' token
#include "Soldier.h"
// your code here
Soldier::Soldier(int num) {
int i;
for (i=1; i<=num; i++) {
list.push(i);
}
};
bool Soldier::check(vector<int> arrange) {
return true;
};
Been trying for hours, at my wits end. Thanks.
vector isn't declared:
#include <vector>