Error using cout with char*
Feb 3, 2016 at 9:29am UTC
Hi, I cannot seem to understand why am I getting this error:
\main.cpp(8): error C3867: 'Mammal::getName': non-standard syntax; use '&' to create a pointer to member
Main Class:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Mammal.h"
#include "Dog.h"
#include <iostream>
int main() {
Mammal mammal = Dog("Doggie Name" , 12, 3);
std::cout << "Printing doggie name! " << mammal.getName << std::endl;
system("pause" );
return 0;
}
Mammal.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
class Mammal {
public :
Mammal(char * mammalName, int weight, int speed);
~Mammal();
int getWeight();
int getSpeed();
char * getName();
private :
char * name;
int speed;
int weight;
};
Mammal.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "Mammal.h"
Mammal::Mammal(char * mammalName, int mammalWeight, int mammalSpeed)
{
name = mammalName;
weight = mammalWeight;
speed = mammalSpeed;
}
Mammal::~Mammal()
{
delete name;
weight = 0;
speed = 0;
}
int Mammal::getSpeed()
{
return speed;
}
int Mammal::getWeight() {
return weight;
}
char * Mammal::getName() {
return name;
}
Dog.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "Dog.h"
#include <iostream>
Dog::Dog(char * dogName, int dogWeight, int dogSpeed)
:Mammal(dogName, dogWeight, dogSpeed)
{}
Dog::~Dog() {
std::cout << "Dog killed!" << std::endl;
}
void Dog::bite() {
std::cout << "BITE!" << std::endl;
}
Last edited on Feb 3, 2016 at 9:30am UTC
Feb 3, 2016 at 9:31am UTC
You need to use parentheses if you wish to call the function.
std::cout << "Printing doggie name! " << mammal.getName() << std::endl;
Feb 3, 2016 at 10:11am UTC
Oh fail. Thank you c:
Topic archived. No new replies allowed.