Ammar T. Ammar T. - 3 months ago 7
C++ Question

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;
};

Answer

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??
};
Comments