0

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;
};
4
  • 2
    Needs jalapeno sauce. Commented Apr 6, 2016 at 23:12
  • 1
    You need to define all the functions in the header and in the cpp. Google a sample header program Commented Apr 6, 2016 at 23:13
  • Your burrito has no variables (data members) declared in the class. Your variables method has local variables that disappear after execution leaves the method. Commented Apr 6, 2016 at 23:19
  • Your source file has private section and }; not associated with anything. Maybe you wanted to move these lines to the end of your Burrito. Commented Apr 6, 2016 at 23:20

4 Answers 4

1

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??
};
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this! Sorry for this mess, I'm new to c++
A common coding guideline is to provide parameter names in the declarations of methods.
@AmmarT glad it helped. Please mark it as an answer if you see it working :)
Do NOT put using namespace std; in the header file, this creates namespace pollution - see here: stackoverflow.com/questions/1452721/…
But this is for beginners? @archbishopofbanterbury
|
0

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

9 Comments

this is too much for a beginner.
set_name needs a void return type in header file :)
@FirstStep: How is this too much for a beginner? I took the OP's code and organized it correctly.
@FirstStep I have no idea what either of those are, thats why I went with yours since it didn't add anything I didn't know.
@FirstStep: Then he has a poor education. I recommend that references be taught before function creation.
|
0

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

2 Comments

the member function setName() needs a string as a parameter :)
@FirstStep Were you pointing to the setName() on the first line? I just edited it
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.