Fantastic
15.5.2011, 9:39
Dugo ni ja nisam postavljao pitanja ovde pa ajd malo :) .
Pokusavam da napravim custom string klasu u c++ koja ce naslediti normani std::string
ali imam problem :(
kada pokusam kompajlirati dobijem
main.cpp: In function 'int main()':
main.cpp:8: error: conversion from 'const char [6]' to non-scalar type 'Stefy::String' requested
ovo je main.cpp
#include "/usr/include/sct/stefy_string.h"
#include <iostream>
using namespace std;
int main() {
Stefy::String s;
cin >> s;
Stefy::String t = "stefy";
if (s.contains(t))
cout << "EXTRA\n";
return 0;
}
sct/stefy_string.h
#ifndef STEFY_STRING_H
#define STEFY_STRING_H
//#include <string>
#include "stefy_stdlib.h"
//namespace Stefy {
STEFY_NAMESPACE_BEGIN
class String : public std::string
{
public:
bool starts_with(std::string w);
bool ends_with(std::string w);
int diff(Stefy::String other);
int diff(const std::string& other);
bool contains(Stefy::String t);
//bool contains(std::string t);
};
//}
STEFY_NAMESPACE_END
#endif //STEFY_STRING_H
i sct/stefy_string.cpp
#include "stefy_string.h"
bool Stefy::String::starts_with(std::string w)
{
return substr(0, w.size()) == w;
}
bool Stefy::String::ends_with(std::string w)
{
return substr(size() - w.size(), w.size()) == w;
}
int Stefy::String::diff(Stefy::String other)
{
Stefy::String c = other;
int min_size = 0;
int max_size = 0;
if (size() < c.size()) {
min_size = size();
max_size = c.size();
}
else {
min_size = c.size();
max_size = size();
}
int e = max_size - min_size;
for (int i = 0; i<min_size; i++)
{
if (at(i) != c.at(i))
e++;
}
return e;
}
/*int Stefy::String::diff(const std::string& other)
{
Stefy::String c = other;
int min_size = 0;
int max_size = 0;
if (size() < c.size()) {
min_size = size();
max_size = c.size();
}
else {
min_size = c.size();
max_size = size();
}
int e = max_size - min_size;
for (int i = 0; i<min_size; i++)
{
if (at(i) != c.at(i))
e++;
}
return e;
}*/
bool Stefy::String::contains(Stefy::String t)
{
std::string o = t.c_str();
int s = o.size();
for (int i = 0; i + s <= size(); i++)
{
if (substr(i, s) == o)
{
return true;
}
}
return false;
}
/*bool Stefy::String::contains(std::string t)
{
int s = t.size();
for (int i = 0; i + s <= size(); i++)
{
if (substr(i, s) == t)
{
return true;
}
}
return false;
}*/
u cemu je problem ????
Pokusavam da napravim custom string klasu u c++ koja ce naslediti normani std::string
ali imam problem :(
kada pokusam kompajlirati dobijem
main.cpp: In function 'int main()':
main.cpp:8: error: conversion from 'const char [6]' to non-scalar type 'Stefy::String' requested
ovo je main.cpp
#include "/usr/include/sct/stefy_string.h"
#include <iostream>
using namespace std;
int main() {
Stefy::String s;
cin >> s;
Stefy::String t = "stefy";
if (s.contains(t))
cout << "EXTRA\n";
return 0;
}
sct/stefy_string.h
#ifndef STEFY_STRING_H
#define STEFY_STRING_H
//#include <string>
#include "stefy_stdlib.h"
//namespace Stefy {
STEFY_NAMESPACE_BEGIN
class String : public std::string
{
public:
bool starts_with(std::string w);
bool ends_with(std::string w);
int diff(Stefy::String other);
int diff(const std::string& other);
bool contains(Stefy::String t);
//bool contains(std::string t);
};
//}
STEFY_NAMESPACE_END
#endif //STEFY_STRING_H
i sct/stefy_string.cpp
#include "stefy_string.h"
bool Stefy::String::starts_with(std::string w)
{
return substr(0, w.size()) == w;
}
bool Stefy::String::ends_with(std::string w)
{
return substr(size() - w.size(), w.size()) == w;
}
int Stefy::String::diff(Stefy::String other)
{
Stefy::String c = other;
int min_size = 0;
int max_size = 0;
if (size() < c.size()) {
min_size = size();
max_size = c.size();
}
else {
min_size = c.size();
max_size = size();
}
int e = max_size - min_size;
for (int i = 0; i<min_size; i++)
{
if (at(i) != c.at(i))
e++;
}
return e;
}
/*int Stefy::String::diff(const std::string& other)
{
Stefy::String c = other;
int min_size = 0;
int max_size = 0;
if (size() < c.size()) {
min_size = size();
max_size = c.size();
}
else {
min_size = c.size();
max_size = size();
}
int e = max_size - min_size;
for (int i = 0; i<min_size; i++)
{
if (at(i) != c.at(i))
e++;
}
return e;
}*/
bool Stefy::String::contains(Stefy::String t)
{
std::string o = t.c_str();
int s = o.size();
for (int i = 0; i + s <= size(); i++)
{
if (substr(i, s) == o)
{
return true;
}
}
return false;
}
/*bool Stefy::String::contains(std::string t)
{
int s = t.size();
for (int i = 0; i + s <= size(); i++)
{
if (substr(i, s) == t)
{
return true;
}
}
return false;
}*/
u cemu je problem ????