Reputation: 79
I'm working on learning c++, I already know java, c# +++. So I thought it would be a walk in the park, but no!! What I'm trying to do is make a simple object("test"), where I can add strings to the vector "data" through the add function, and then make some kind of print function that prints it. I've tried make something shown underneath, any input would be appreciated :)
Call:
test a;
a.add("blabla");
a.print();
test.h:
class test{
vector<string>* data;
std::string str;
public:
test();
void add(std::string t);
};
**test.cpp:**
test::test() {
data = new vector<string>;
}
void test::add(std::string t) {
data->pushback(t);
}
void test::print() {
cout << data[0];
}
Upvotes: 0
Views: 113
Reputation: 6755
Did you remember to include test.h in the file with your main method?
Also, you either need to use std::cout
or have the line using namespace std;
in your code.
In addtion, the push-back method for vector is push_back()
, not pushback()
.
Finally, since you're using the new
keyword in your constructor, you'll need a destructor as well that would look something like this:
test::~test()
{
delete data;
}
This is a very important thing to remember when you are doing C++ programming as opposed to Java or C#. C++ implementations do not automatically garbage collect, so you need to take care of releasing your allocated memory on your own.
EDIT:
Also, one of the answers below points out another issue. You forgot to dereference the vector pointer before indexing into it. You need to print out (*data)[0]
. Moral of the story is, it would definitely be easier and better if you made data a vector
rather than a pointer to vector
.
Upvotes: 0
Reputation: 603
I think you forgot to de-reference the vector pointer in test::print
void test::print() {
cout << (*data)[0];
}
*Also it is important to remember to delete objects created with new
Upvotes: 0
Reputation: 20730
One of the fundamental thing that makes C++ different from Java and C# is that in C++ all classes are "value classes". The other is that dynamic memory is "manual".
From the second point descends the fact that every time you call new
, you must think about who and when will call delete
.
From the first point descends that vector
and string
behave as values, and that the dynamic memory required to handle their growing and shrinking is managed by the classes themselves. There is no need to allocate them dynamically.
You must also take care of the headers where classes are declared (that must be #include
d where used) and of the namespaces that contains the declaration you use (either by declare a using
or by callign them explicitly).
The following code does (in a single file) what you wanted.
#include <vector>
#include <string>
#include <iostream>
class test
{
public:
void add(std::string t);
void print() const;
private:
typedef std::vector<std::string> data_t;
data_t data;
};
void test::add(std::string t)
{ data.push_back(t); }
void test::print() const
{
for(data_t::size_type i=0; i<data.size(); ++i)
std::cout << data[i] << std::endl;
}
int main()
{
test a;
a.add("first");
a.add("second");
a.add("third");
a.print();
return 0;
}
There are more canonical
ways to handle printing (by parametrising the output stream and making a << operator overload that treats an "a" as another ordinary value).
But right now, compare this code (that the most similar to your one) with yours and find out what's not just syntactically different.
EDIT
I don't know if you are using C++11 or not... There are two possible enhancement, here:
If stuck to C++03,
void add(sd::string t)
cand be better parametrised as
void add(const std::string& t)
// ^^^^^ ^
If using C++11, since you don't need t
anymore after giving it to push_back,
void test::add(std::string t)
{ data.push_back(std::move(t)); }
and in this case don't use const&
.
Upvotes: 2