Ammar T.
Ammar T.

Reputation: 5

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

Upvotes: 0

Views: 133

Answers (4)

user6169399
user6169399

Reputation:

This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:

#include <iostream> 
#include <string> 
using namespace std;
class burrito {
public:
    void setName(string s);
    string getName();
private:
    string name;
};

void burrito::setName(string s) {
    name = s;
}
string burrito::getName() {
    return name;
}

int main() {
    burrito a;
    a.setName("Ammar T.");
    std::cout << a.getName() << "\n";
    return 0;
}

Upvotes: 0

Khalil Khalaf
Khalil Khalaf

Reputation: 9407

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

Upvotes: 1

nivbala
nivbala

Reputation: 3

The header file only has a constructor for the class. The member functions

   setName(string) and getName()       

are not declared in the header file and that is why you get the error. Also, you need to specify the return type for functions.

One way to do this would be

 //Header 
 //burrito.h   
class burrito{
   private:
   string burrito_name; 
   public:
       burrito();
       string getName(); 
       void setName(string);
             }

//burrito.cpp
#include "burrito.h"
#include <iostream>

using namespace std;

string burrito::getName()
{
return burrito_name; 
}

void burrito::setName(string bname)
{
 bname =burrito_name;
} 

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57718

Your poor little burrito is confused. Confused burritos can't help much.

You may want your burrito declaration as:

class Burrito
{
public:
    Burrito();
    void set_name(const std::string& new_name);
    std::string get_name() const;
private:
    std::string name;
};

The methods could be defined in the source file as:

void
Burrito::set_name(const std::string& new_name)
{
  name = new_name;
}

std::string
Burrito::get_name() const
{
  return name;
}

Upvotes: 0

Related Questions