To make multigraphs which is weighted also , i do following thing
#include <iostream>
#include <vector>
using namespace std;
struct maps
{
vector<char> weight(10); //to store weight of self-loops and multi-edges
};
int main()
{
maps m1[101][101], m2[101][101];
return 0;
}
but I get following errors:
error: expected identifier before numeric constant
error: expected ‘,’ or ‘...’ before numeric constant
How can I fix this?
As Ade YU mentioned, do not define the size of your weight vector in it's declaration. Instead, do it in the initializer list in the constructor. This should do what you're looking for:
#include <iostream>
#include <vector>
using namespace std;
struct maps
{
maps() : weight(10) {}
vector<char> weight; //to store weight of self-loops and multi-edges
};
int main()
{
maps m1[101][101], m2[101][101];
return 0;
}
You need to initialize the vector in the constructor. Try this:
#include <iostream>
#include <vector>
using namespace std;
struct maps{
maps() : weight(10) {}
vector<char> weight;//to store weight of self-loops and multi-edges
};
int main()
{
maps m1[101][101], m2[101][101];
return 0;
}
Related
I'm trying to build a class named "Tombola" which should contain as private variable an empty vector. This should be filled at runtime through the class member Tombola.estrai(), which generates a random number and insert it inside the vector named "order" by the method order.push_back(number). This is the class definition in the tombola.h header:
#ifndef TOMBOLA_H
#define TOMBOLA_H
#include <cstdlib>
#include <vector>
using namespace std;
class Tombola {
private:
bool on_off[90];
int tabellone[9][10];
int x_max = 9;
int y_max = 10;
vector<int> order;
public:
Tombola();
~Tombola();
void nuovo();
int estrai();
bool completato();
void stampa();
void stampa_tab();
};
#endif
And this is the implementation of constructor/destructor and Tombola::estrai() inside tombola.cc:
#include "tombola.h"
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <iostream>
using namespace std;
Tombola::Tombola () {
vector<int> ord;
order = ord;
int z=1;
for(int i=0;i<90;i++) {
on_off[i] = false;
}
for(int j=0;j<=x_max;j++) {
for (int k=0;k<=y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
}
Tombola::~Tombola() {
cout << "Tombola is destroyed" << endl;
}
int Tombola::estrai() {
srand(time(NULL));
int estrazione = int(ceil(rand()/double(RAND_MAX)*90));
on_off[estrazione]==true;
order.push_back(estrazione);
return order.back();
}
and this is the call to the method in the main.cpp file:
#include "tombola.h"
#include <cstdlib>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main () {
Tombola natale;
cout << natale.estrai();
}
When I compile the program everything goes fine, but when I execute the main I get a segmentation fault error which seems to be due to some sort of allocation error when trying to store the item inside the order vector, as reported by the debugger. Could someone explain to me how to solve the error and why the error occours? Thank you.
The reason of segmentation fault is in the constructor. You have to change for(int j=0;j<=x_max;j++) to for(int j=0;j<x_max;j++) in order not to cross the bounds of the array.
for(int j=0;j<x_max;j++) {
for (int k=0;k<y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
However, there are also some minor issues in the code that are worth being mentioned
declaring default-initialized ord vector and assigning it to order is pointless because order is already default-initialized.(See member initializer list for more information).
using namespace std; in a header file is a terrible idea, because if you had a large codebase, and had multiple source files where you want to include that header, everywhere the using statement will be applied, which probably is not desired.
I need your help please especially to know can I convert a string Variable in an enum variable.
Here is my code:
deco_fill.h
#include <iostream>
using namespace std;
#include <string.h>
class A{
class B{
public:
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
public:
tStrict mType;
void setStrict(tStrict& newStrict ){
return mType=newStrict;
}
}
}
test.h
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
class C
{
public:
A::B::tStrict CnvStrToEnum(const string& str); //This method will return a tStrict type
}
test.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <test.h>
#include <deco_fill.h>
A::B::tStrict C::CnvStrToEnum(const string& str)
{
if (str=="ST_USE")
return ST_USE;
else if (str=="ST_DEL")
return ST_DEL;
else
return ST_UNKNOWN;
}
test_set.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
#include <test.h>
string st=ST_USE;
A::B::tStrict strictType=CnvStrToEnum(st);
setStrict(strictType);//I want here to use the setStrict methode to set a new variable of type enum with that. This part is not important
I have a compile error in test.cpp like ST_DEL, ST_USE and ST_UNKNOWN were not declared. What's do I need here and how can I correctly the string type in my enum type. Thanks for your help.
enum is for numeric constants (not strings), so you can't write
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
Also note it's comma (NOT semicolon) after each enum constant.
So instead you should write:
enum tStrict{
ST_UNKNOWN=-1,
ST_USE,
ST_DEL
};
Usually you can then convert enum constants to string counterparts:
const char *tStrictStr( const enum tStrict t )
{
switch( t )
{
case ST_UNKNOWN : return "ST_UNKNOWN" ;
case ST_USE : return "ST_USE" ;
case ST_DEL : return "ST_DEL" ;
default : return "ST_UNKNOWN" ;
}
}
I have this class (hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
And a function header file (functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
And the main file:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
I created the hashMap class and I want to be able to return a hashMap object, but this is giving me an error of "Error C4430 missing type specifier - int assumed." What am I doing wrong?
I put the code in files and nicely asked the compiler to do its job. This is the first warning from the list:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
The compiler doesn't know what hashMap is. When it reaches the line with the error, the hashMap symbol was not yet declared or defined.
You shouldn't define functions in header files.
Rename functions.h to functions.cpp, add #include "functions.h" at the end of the list of includes.
Create a new file functions.h that contains only the declarations of the functions (the function header) and the types they use:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
You are including functions.h from hashmap.h BEFORE the hashMap class is defined. As such, when the compiler reads functions.h, the hashMap class is not defined.
For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:
I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}
I'm having trouble with the following code and can't seem to figure out what is wrong
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
double distance(int a, int b)
{
return fabs(a-b);
}
int main()
{
vector<int> age;
age.push_back(10);
age.push_back(15);
cout<<distance(age[0],age[1]);
return 0;
}
The error lies at calling of function distance.
/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
test.cpp:18:30: instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:170:53: error: ‘int’ is not a class, struct, or union type
You are trying to override std::distance function, try removing "using namespace std" and qualifying cout and endl with std::
#include <iostream>
#include <cmath>
#include <vector>
double distance(int a, int b)
{
return fabs(a-b);
}
int main()
{
std::vector<int> age;
age.push_back(10);
age.push_back(15);
std::cout<< distance(age[0],age[1]);
return 0;
}
The std::distance is used to count the number of elements in a container within a specified range. You can find more about it here.
Or you can rename your distance function if you want to introduce the std:: namespace:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
double mydistance(int a, int b)
{
return fabs(a-b);
}
int main()
{
vector<int> age;
age.push_back(10);
age.push_back(15);
cout<<mydistance(age[0],age[1]);
return 0;
}
This will make your code work, but it is not recommended to have "using namespace" declarations before definitions. When you write your code, you should avoid the second option, it's shown here only as an alternative for your code example.
How about
cout<< ::distance(age[0],age[1]);
(other answers already suggest removing the using directive).
Don't use using namespace std when you're creating your own function called distance, because your call to distance is looking for std::distance and not your distance function.
You could also do this:
namespace foo
{
double distance(int a, int b)
{
return fabs(a-b);
}
}
int main()
{
foo::distance(x,y); //now you're calling your own distance function.
}
Alternatively, you can use
using foo::distance; // OR:
using namespace foo;
(distance)(x,y); // the (parens) prevent ADL