the error by the compiler is

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe Untitled1.o plants.o  -o "Project1.exe" -L"C:/Dev-Cpp/lib"  

Untitled1.o(.text+0x182):Untitled1.cpp: undefined reference to `Plants::Plants()'
collect2: ld returned 1 exit status

make.exe: *** [Project1.exe] Error 1

Execution terminated

main code is

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;
using std::getline;

#include "Plants.h"

int main()
{
 string kind;
 Plants plant1;
 cout<<"Enter plant kind"<<endl;
 cin >> kind;
 plant1.setStats(kind);
 cout << plant1.getAge() << " " << plant1.getSize() << " " << plant1.getName() << endl;
 
 }

header file

#include <string>
using std::string;

class Plants
{
	public:
		
	void setStats(string);
    int getSize();
    int getAge();
    string getName();		

private:
 string kind;
 string name;
 int age;
 int size;
};

and .cpp file

// Class automatically generated by Dev-C++ New Class wizard
#include <iostream>

#include <cstdlib>
using std::rand;

#include "Plants.h"


void Plants::setStats(string kind)
{
 name = kind;
 size = 1 + rand()%10;
 age = 0;
}

int Plants::getSize()
{
 return size;
}

int Plants::getAge()
{
 return age;    
}

string Plants::getName()
{
 return name;
}

Recommended Answers

All 3 Replies

Try using the this operator for your class variables to avoid confusion as you seem to be using a type String variable named kind in both your class and your main program.

Also, your method signature in your class is wrong. You are missing the parameter name for your string.
I hope that solves it.
Greetings from a fellow Greek ;)

The OP's original question was already answered in this thread.

But I think I need to address this:

Try using the this operator for your class variables to avoid confusion as you seem to be using a type String variable named kind in both your class and your main program.

Also, your method signature in your class is wrong. You are missing the parameter name for your string.
I hope that solves it.
Greetings from a fellow Greek ;)

Since "kind" is not declared globally, there shouldn't be any naming issues between main() and the class' member variables due to scoping rules. The declarations and definitions are fine. Unless an array is being passed in, a variable's name is not required in the declaration, only the type is required. Including the name in the declaration is mostly irrelevant except for documentation purposes.

However, this:

void Plants::setStats(string kind)
{
name = kind;
size = 1 + rand()%10;
age = 0;
}

is an issue. By defining a member function's parameter as "kind" you are masking the class' member variable "kind" so that it can't be seen from the function without the use of this->. A better name for this parameter would be "newName" so that your aren't hiding "kind". This is a convention you should consider for any "setter"-style member function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.