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
Related
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 months ago.
Improve this question
I will write the code of the header file and two .cpp files I created under the same folder below. And I tried this under Visual Studio, gcc-gnu, and dev c++. I am getting the following error in the file employe.cpp:
expected initializer before '.' token
employee.h:
#ifndef employee
#define employee
#include <iostream>
using namespace std;
class employe{
public:
string name;
int id;
int salary;
void showInfos();
};
#endif
employe.cpp:
#include "employee.h"
#include <iostream>
using namespace std;
void employe.showInfos(){
cout<<"Ad:"<<employe.name<<endl<<"Id:"<<employee.id<<endl<<"Salary:"<<employee.salary;
}
main.cpp:
#include <iostream>
#include "employee.h"
#include <iostream>
int main(){
employe.id=21;
cout<<employe.id;
return 0;
}
main.cpp errors:
expected unqualified-id before '.' token
expected primary-expression before '.' token
I meant to create my own header file and use it. But this happened.
employe.cpp fails because you are qualifying everything incorrectly. It needs to look more like this:
#include "employee.h"
#include <iostream>
using namespace std;
void employe::showInfos(){
cout << "Ad:" << name << endl << "Id:" << id << endl << "Salary:" << salary;
}
main.cpp fails because none of your struct members are static so you can't access any of them on the struct type itself, like you are trying to do. You need to declare an object instance of employe, eg:
#include "employee.h"
int main(){
employe emp;
emp.name = "Joe";
emp.id = 21;
emp.salary = 12345;
emp.showInfos();
return 0;
}
Online Demo
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 1 year ago.
Improve this question
On line five of my code, the compiler raises an error. "error: expected unqualified-id
std::string send(message); {"
What error is this? How do I fix it?
Here's my code:
#include <iostream>
#include <fstream>
std::string message = "blank";
std::string send(message); { // here it raises the error
std::ofstream MyFile("messagepy.txt");
std::ofstream MyFile; << "Files can be tricky, but it is fun enough!";
}
// // // // // // // // // // // // // //
int main() {
std::cout << "defining is done.";
}
You have some syntax errors in there. Not sure what your code is meant to do, but here is my guess:
#include <iostream>
#include <fstream>
#include <string>
void send(std::string message)
{
std::ofstream MyFile("messagepy.txt");
MyFile << message;
MyFile << "Files can be tricky, but it is fun enough!";
}
int main()
{
std::string message = "blank";
send(message);
std::cout << "defining is done." << std::endl;
}
I believe the error is caused because you didn't #include <string>.
But there are other problems too. It sort of looks like you meant to create a send function but the semicolon ; is causing it to look like a declaration of a global variable send constructed with message, and then a code block which is a syntax error because it isn't part of a function.
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;
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
File sys.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <fstream>
using namespace std;
[...]
struct kmph_in_mps
{
int kmph[4];
int result[4];
void kmph_erfassen()
{
for (size_t i = 0; i < 4; ++i)
{
cin >> kmph[i];
}
}
void mps_erfassen(int kmph, double result)
{
result = kmph / 3.6;
}
void ergebniss_ausgeben()
{
cout << endl << kmph << "Km/h sind " <<result << " Meter pro Sekunde\n";
}
};
[...]
File main.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include "sys.cpp"
#include <fstream>
kmph_in_mps c;
[...]
void Kmph_in_mps()
{
system("cls");
cout << "\nKm/h: ";
c.kmph_erfassen();
c.mps_erfassen(int kmph, double result);
c.ergebniss_ausgeben();
t.beenden();
}
[...]
Errors:
Error: expected primary-expression before “int”
Error: expected primary-expression before “double”
I am a learning C++, and I dont get what to do now.
I am searching for answers at the internet and here, but I dont realy find the right one.
Which primary expression do I have to write before "in" and "double"?
Or am I doing everything completly wrong, like a beinner does? :P
Edits:
I tryed return result, but it seems to be not allowed in a void.
I already tryed c.mps_erfassen(); but it is gives me an error, too: error: no matching function for call to `kmph_in_mps::mps_erfassen()'|
I already tryed c.mps_erfassen(kmph, result); but then I do not declare them both in this scope. They are declared in the other file (sys.cpp). :S
c.mps_erfassen(int kmph, double result);
//^^^remove int and double
When you call a function, you should not put the type before the parameters.
This
c.mps_erfassen(int kmph, double result);
should be
c.mps_erfassen(kmph, result);
Let function deduce the type :)
EDITED IN RESPONSE TO COMMENT:-
You are creating object of struct in main.cpp while it's definition is in sys.cpp. How would main.cpp would get to know what your struct means.
For better design, place declaration of struct in header file say sys.h, then define required members in .cpp file say sys.cpp ( you have to include sys.h ). And then use that struct in your main.cpp ( again you have to include sys.h over here ).
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;
...