C++ I keep getting "was not declared in this scope error" - c++

So, I have a header file and a class file. But when I compile puzzle.cpp, I keep getting get_solution was not declared in this scope. I don't get why that error occurs since its inside the same class so I can call any function inside the same class. Can anyone please help me on this? Thanks!
puzzle.h
#ifndef PUZZLE_H
#define PUZZLE_H
#include<iostream>
#include <string>
#include <vector>
class puzzle{
private:
std::string _solution;
std::vector<bool> _guesses;
public:
puzzle(std::string solution);
std::string get_solution(){return _solution;}
bool guess(char c);
bool solve(std::string proposed_solution);
std::string to_string();
};
#endif
puzzle.cpp
#include <iostream>
#include "puzzle.h"
#include <string>
#include <vector>
using namespace std;
puzzle::puzzle(std::string solution) {
_solution = solution;
for(int i = 0; i < 256; i++)
_guesses.push_back(false);
}
bool puzzle::guess(char c){
int num = c;
if(c<='z' || c>='a')
if(_guesses.at(c) == false){
_guesses.at(c) == true;
return true;
}
return false;
}
bool solve(string proposed_solution){
string test = get_solution();
if(proposed_solution.compare(test) == 0)
return true;
return false;
}
string to_string(){
int len = get_solution().length();
return "";
}

It looks like you've forgotten to make solve and to_string member functions:
Change
string to_string(){ ...
bool solve(string proposed_solution){ ...
^^^
To
string puzzle::to_string(){ ...
bool puzzle::solve(string proposed_solution){ ...

Your function bool solve(string proposed_solution) does not define a member function of puzzle but a "plain" function; Hence, get_solution(); within its body is not recognized as a member of puzzle, too. You'll have to write bool puzzle::solve(string proposed_solution) { ... and it should work.

solve and to_string are supposed to be methods, so you need to prefix them with the class' name followed by two colons (i.e., puzzle::):
bool puzzle::solve(string proposed_solution){
// Code ...
}
string puzzle::to_string(){
// Code ...
}

Related

Enum in a class with strings

I'm trying to implement a class (C++) with an enum (with the permitted parameters). I got a working solution, but if I try to extend the functionality I get stuck.
Header data_location.hpp
class DataLocation
{
private:
public:
enum Params { model, period };
std::string getParamString(Params p);
};
Program data_location.cpp
string DataLocation::getParamString(Params p){
static const char * ParamsStrings[] = {"MODEL", "PERIOD"};
return ParamsStrings[p];
}
The array ParamsStrings should be generally available in the class, because I need a second method (with inverse function) returning the enum value given a string.
If I try to define the array in the header I get the error:
in-class initialization of static data member ‘const char* DataLocation::ParamsStrings []’ of incomplete type
Why is the type incomplete? The compiler is for sure able to counts the strings in the array, isn't it?
In case there is no way to get my code working, is there an other way? With 1) no XML; 2) no double definition of the strings; 3) not outside the class; 4) no in code programmed mapping.
In class (header) use keyword static and initialize it outside (.cpp) without the static keyword:
class DataLocation {
public:
enum Params { model, period };
string getParamString(Params p);
static const char* ParamsStrings[];
// ^^^^^^
};
const char* DataLocation::ParamsStrings[] = {"MODEL", "BLLBLA"};
//^^^^^^^^^^^^^^^^^^^^^^^^
The code you have posted is perfectly fine.
Here's the proof:
#include <iostream>
#include <string>
struct DataLocation
{
enum Params { model, period };
std::string getParamString(Params p){
static const char * ParamsStrings[] = {"MODEL", "PERIOD"};
return ParamsStrings[p];
}
};
int main()
{
auto a = DataLocation();
std::cout << a.getParamString(DataLocation::model) << std::endl;
return 0;
}
The error message you are getting is not to do with definition of a static data member in an inline function - that's allowed.
There's something else you're not showing us.
The main issue in my question (the second part) was that if I split the class in .hpp and .cpp the definition of the array (I mixed *char and string) has also to be split:
// data_location.hpp
class DataLocation {
static const char * ParamsStrings[];
}
// data_location.cpp
const char * ParamsStrings[] = {"MODEL", "PERIOD"};
At the end I introduced a consistency check to be sure that the number of values in enum growths as the number of strings. Because the array in C++ is somehow limited I had to go for a std::vector (to get the size).
Code for data_location.hpp
#ifndef DATA_LOCATION_HPP_
#define DATA_LOCATION_HPP_
#include <string>
#include "utils/dictionary.hpp"
extern const char* ENV_DATA_ROOT;
struct EDataLocationInconsistency : std::runtime_error
{
using std::runtime_error::runtime_error;
};
struct EDataLocationNotValidParam : std::runtime_error
{
using std::runtime_error::runtime_error;
};
class DataLocation
{
private:
std::string mRootLocation;
static const std::vector<std::string> msParamsStrings;
static bool msConsistenceCheckDone;
public:
DataLocation();
std::string getRootLocation();
std::string getLocation(Dictionary params);
enum Params { model, period, LAST_PARAM};
std::string Param2String(Params p);
Params String2Param(std::string p);
};
#endif
Code for data_location.cpp
#include "data_location.hpp"
#include <string>
#include <cstdlib>
using namespace std;
const char* ENV_DATA_ROOT = "DATA_ROOT";
bool DataLocation::msConsistenceCheckDone = false;
DataLocation::DataLocation() {
mRootLocation = std::getenv(ENV_DATA_ROOT);
if (not msConsistenceCheckDone) {
msConsistenceCheckDone = true;
if (LAST_PARAM+1 != msParamsStrings.size()) {
throw(EDataLocationInconsistency("DataLocation: Check Params and msParamsStrings"));
}
}
}
string DataLocation::getRootLocation() {
return mRootLocation;
}
string DataLocation::getLocation(Dictionary params) {
// to do
return "";
}
const vector<string> DataLocation::msParamsStrings = { "MODEL", "PERIOD", ""};
string DataLocation::Param2String(Params p) {
if (p>=msParamsStrings.size()) {
throw(EDataLocationNotValidParam("Parameter not found"));
}
return msParamsStrings[p];
}
DataLocation::Params DataLocation::String2Param(string p) {
for (int i = 0; i < msParamsStrings.size(); i++) {
if (p == msParamsStrings[i])
return (Params)i;
}
throw(EDataLocationNotValidParam("Parameter not found"));
}
And also a unit test:
#include <boost/test/unit_test.hpp>
#include "data_location.hpp"
#include <string>
using namespace std;
BOOST_AUTO_TEST_SUITE( data_location )
BOOST_AUTO_TEST_CASE(data_location_1) {
DataLocation dl;
auto s = dl.getRootLocation();
BOOST_CHECK_EQUAL(s, "/home/tc/data/forex" );
BOOST_CHECK_EQUAL(dl.Param2String(DataLocation::period),"PERIOD");
BOOST_CHECK_EQUAL(dl.String2Param("PERIOD"),DataLocation::period);
BOOST_CHECK_THROW(dl.String2Param("SOMETHING"), EDataLocationNotValidParam);
BOOST_CHECK_THROW(dl.Param2String((DataLocation::Params)100), EDataLocationNotValidParam);
}
BOOST_AUTO_TEST_SUITE_END()
C++ is very picky about what it will let you initialize inside of a class definition; there are some particularly non-intuitive rules surrounding static members. It all has to do with the ODR, and why all the rules are the way they are is not especially important.
To cut to the chase, making your array a static constexpr const member should shut the compiler up. With the C++11 standard, the restrictions were relaxed a bit, and one of the new stipulations was that static constexpr members can be initialized inline. This is perfect for your application, since the strings in your array are compile-time constants.
The recent g++ compiler which support C++0x or later compiles thus code. Pure C compile compiles, too. Because strings in initialization like {"MODEL", "PERIOD"}; implemented as const char * pointer to the char array.

No member named 'name' in namespace 'namespace'

I can't for the life of me figure out why this error is being generated as I'm pretty sure the syntax is correct (obviously I'm wrong!). So I figured I'd see if anyone here could point it out for me.
main.cpp
#include "Object.h"
int main(){
out = json::readJSON(data_dir + "a2-empty_array_with_empty_object.json", e, debug);
}
Object.h
namespace json{
template<typename T>
std::string readJSON(std::string jsonFile, T& object, bool debug = false, char delimiter = ',') {}
}
I'm basically getting this error, when clearly the function is in the namespace. Why does it refer to the function as a member? Maybe there is something else going on here...
Error:
a2main.cpp:66:21: error: no member named 'readJSON' in namespace 'json'
out = json::readJSON(data_dir + "a2-cartoons.json", c, debug, '|');
You are probably not including the header files correctly.
The following code compiles (with both clang and gcc) and runs fine
#include <string>
namespace json
{
template<typename T>
std::string readJSON(std::string jsonFile, T& object, bool debug = false, char delimiter = ',')
{
return "Hello"; //This function should return a string
}
}
int main()
{
std::string data_dir = "test-";
int e = 3;
bool debug = false;
std::string out = json::readJSON(data_dir + "a2-empty_array_with_empty_object.json", e, debug);
return 0;
}
I hope this helps.

C++ Error: Was not declared in the scope

Hey guys I'm working on a project and I was doing pretty well until I hit this wall..
I am getting two errors:
error: 'binarySearch' was not declared in this scope
error: 'addInOrder' was not declared in this scope
Here are my files, I've tried quite a few things with no avail. Help would be much appreciated.
histogram.cpp
#include "histogram.h"
#include "countedLocs.h"
//#include "vectorUtils.h"
#include <string>
#include <vector>
using namespace std;
void histogram (istream& input, ostream& output)
{
// Step 1 - set up the data
vector<CountedLocations> countedLocs;
// Step 2 - read and count the requested locators
string logEntry;
getline (input, logEntry);
while (input)
{
string request = extractTheRequest(logEntry);
if (isAGet(request))
{
string locator = extractLocator(request);
int position = binarySearch (countedLocs,
CountedLocations(locator, 0));
/** Hint - when looking CountedLocations up in any kind
of container, we really don't care if the counts match up
or not, just so long as the URLs are the same. ***/
if (position >= 0)
{
// We found this locator already in the array.
// Increment its count
++countedLocs[position].count;
}
else
{
// This is a new locator. Add it.
CountedLocations newLocation (locator, 1);
addInOrder (countedLocs, newLocation);
}
}
getline (input, logEntry);
}
// Step 3 - write the output report
for (int i = 0; i < countedLocs.size(); ++i)
output << countedLocs[i] << endl;
}
countedLocs.cpp
#include "countedLocs.h"
#include <iostream>
#include <vector>
using namespace std;
int CountedLocations::binarySearch(const vector<CountedLocations> list, CountedLocations searchItem)
{
//Code was here
}
int CountedLocations::addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value)
{
//Code was here
}
countedLocs.h
#ifndef COUNTEDLOCATIONS
#define COUNTEDLOCATIONS
#include <iostream>
#include <string>
#include <vector>
struct CountedLocations
{
std::string url;
int count;
CountedLocations (){
url = "";
count = 0;
}
CountedLocations(std::string a, int b){
url = a;
count = b;
}
int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);
int binarySearch (const std::vector<CountedLocations> list, CountedLocations searchItem);
};
inline
std::ostream& operator<< (std::ostream &out, CountedLocations& cL)
{
//out << "URL: " << cL.url << " count: " << cL.count << std::endl;
out << "\"" << cL.url << "\"," << cL.count;
return out;
}
#endif
The methods are member methods of CountedLocations... use something.extractLocator and something.binarySearch or make the histogram() to be also a member method of CountedLocations... (something is of type CountedLocations highly possibly will be countedLocs[position])
You have a free function histogram in which you are trying to use two member functions, addInOrder and binarySearch. In order to use them, you need to have an instance of CountedLocations.
If these are some kind of helper functions, which do not depend on the actual CountedLocations instance, I would turn them into static functions like this (you only need to change the header):
static int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);
And then you can call this function by specifying the type of your class:
CountedLocations::addInOrder(...);
You are trying to call member methods of a struct without an object of that type. Strange.
You need to look at what a namespace is.
You declare a class CountedLocations, so far so good. But then you try to use the member functions outside the CountedLocations namespace which will obviously never work.
int position = binarySearch (countedLocs,
CountedLocations(locator, 0));
binarySearch is a member function of the CountedLocations namespace. If you want to call that function you have to create an object that contains a reference to that member function.
CountedLocation myObject;
int position = myObject.binarySearch (countedLocs, CountedLocations(locator, 0));
I dont know if that solves your problem, but you should know this before you even attempt to solve a problem.

Using template in c++ - Returning values from method

I wanna a method that receives a generic type, and a generic type (that's defined in run time). In the example there's if I'm using a string type, It needs to return the first param lenght (in string); If I'm using a int type, needs to return the biggest (int integer).
Have a look:
#include "stdafx.h"
#include <sstream>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <string>
#include <atldbcli.h>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
using namespace std;
class Test
{
public:
template<class T>
T returnVal(T valueOne, T valueTwo);
};
template<class T>
T Test::returnVal(T valueOne, T valueTwo)
{
if(typeid(valueOne) == typeid(string))
{
string teste = valueOne;
int testeInt = teste.size();
ostringstream testeString;
testeString << testeInt;
teste = testeString.str();
return teste;
}
else
return valueOne > valueTwo? valueOne:valueTwo;
}
int main()
{
string reference = "stringVal";
Test ref;
cout << ref.returnVal<string>(reference, "asasas") << endl;
cout << ref.returnVal<int>(10, 485);
getch();
return 0;
}
However, when the main function calls ref.returnVal(10, 485); it's show a message error: 'return' : cannot convert from 'std::string' to 'int'
Does anybody know what's wrong?
Thanks
this is not the proper way to do what you want. You can't use typeid to switch between types and do different operations, because all the different paths still have to be compiled, and inside your if you do return a string while your method returns an int.
google template specialization, that's what you need I guess...
#include <string>
template <class T>
int returnVal(T valueOne, T valueTwo);
template <>
int returnVal<std::string>(std::string valueOne, std::string valueTwo)
{
return (int)valueOne.length();
}
template <>
int returnVal<int>(int valueOne, int valueTwo)
{
return std::max(valueOne, valueTwo);
}
int main()
{
int x = returnVal(std::string("Hello"), std::string("World!"));
int y = returnVal(1,2);
return 0;
}
Unless I'm misunderstanding you, you could achieve this with template specialization?
All previous answers clearly identify the problem. As for a better way to do it:
class Test
{
public:
string returnVal(string valueOne, string valueTwo)
{
string teste = valueOne;
int testeInt = teste.size();
ostringstream testeString;
testeString << testeInt;
teste = testeString.str();
return teste;
}
int returnVal(int valueOne, int valueTwo)
{
return valueOne > valueTwo? valueOne:valueTwo;
}
};
If you instantiate your template for type int, it looks like this:
int Test::returnVal(int valueOne, int valueTwo)
{
if(typeid(valueOne) == typeid(string)) // This will be false
{
string teste = valueOne;
int testeInt = teste.size();
ostringstream testeString;
testeString << testeInt;
teste = testeString.str();
return teste;
}
else
return valueOne > valueTwo? valueOne:valueTwo;
}
The problem is that the then-clause of your if returns a string even though the return type of the function is int. The fact that the then-clause will never execute because typeif(valueOne) can't possible be string doesn't matter because the type checker does not care about that. All he sees is a return statement that returns a string, so that's an error.
To do what you want, you should simply overload your function for strings and remove all the string-specific code from the templated function.

Calling a member function pointer stored in a std map

I'm storing a map in a class that has strings as keys and pointers to member functions as values. I'm having trouble calling the right function throw the function pointer.
Here is the code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Preprocessor;
typedef void (Preprocessor::*function)();
class Preprocessor
{
public:
Preprocessor();
~Preprocessor();
void processing(const string before_processing);
private:
void take_new_key();
map<string, function> srch_keys;
string after_processing;
};
Preprocessor::Preprocessor()
{
srch_keys.insert(pair<string, function>(string("#define"), &Preprocessor::take_new_key));
}
Preprocessor::~Preprocessor()
{
}
void Preprocessor::processing(const string before_processing)
{
map<string, function>::iterator result = srch_keys.find("#define");
if(result != srch_keys.end())
result->second;
}
void Preprocessor::take_new_key()
{
cout << "enters here";
}
int main()
{
Preprocessor pre;
pre.processing(string("...word #define other word"));
return 0;
}
In function Preprocessor::processing if the string is found in the map then, I call the proper function. The problem is that, in this code, Preprocessor::take_new_key is never called.
Where is the mistake ?
Thanks
The correct syntax is this:
(this->*(result->second))();
That is ugly. So lets try this:
auto mem = result->second; //C++11 only
(this->*mem)();
Use whichever makes you happy.
result->second does not call the function pointer. Try ((*this).*result->second)();