Reputation: 53
I am really new to C++ so if it a is really stupid question I am sorry. I am trying to create an Object Dog but it won't allow me to pass in the name into the contractor here is the code
Main.cpp
#include <iostream>
#include <string>
#include "Dog.h"
int main(){
std::string name = "Spike";
Dog *dog = new Dog('Name', 2);
}
Dog.h
#include <string>
class Dog {
public :
std::string name;
int age;
Dog(std::string name , int age);
};
Dog.cpp
#include "Dog.h";
#include <string>;
Dog::Dog(std::string name, int age)
{
Dog::name = name;
Dog::age = age;
}
Upvotes: 1
Views: 79
Reputation: 2106
You are using ::
wrongly. It is for accessing static variables and methods of a class/struct or namespace.
You are trying to access object members, use either .
or ->
For more info: When do I use a dot, arrow, or double colon to refer to members of a class in C++?
Upvotes: 1
Reputation: 16476
The error you are seeing is because you are passing single quoted Name and not the lower cased name variable. That will give you errors. However there are others.
You are trying to access the member variable name by dog::name
. This is incorrect use dog.name
.
So use period and not double colon.
Note: You could also use this->name = name
.
Upvotes: 1