I try to write a program check a string is Palindrone or not using vector and iterator.
I create definition in iterator.h file:-
#pragma once
#ifndef iterator
#define iterator
template<typename Bidrectional>
bool isPalindrone(Bidrectional first, Bidrectional end);
template<typename Bidrectional>
inline bool isPalindrone(Bidrectional first, Bidrectional last)
{
while (true)
{
last--;
if (first == last)
break;
if (*first != *last)
return false;
first++;
if (first == last)
{
break;
}
return true;
}
}
#endif
Now I got so many error in compile time:-
Severity Code Description Project File Line Source Suppression State
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1640 Build
Error C2976 'std::reverse_iterator': too few template arguments c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 656 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 906 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 939 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 943 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 947 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1170 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1186 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1563 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1583 Build
Error C2059 syntax error: '=' c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 654 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 1650 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 2039 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++project C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.32.31326\include\vector 2035 Build
Error picture:-
In main function I call is
#include <iostream>
#include "iterator.h"
#include <vector>
using namespace std;
int main()
{
vector<string> s1;
s1.push_back("Otto");
isPalindrone(s1.begin(), s1.end());
}
I am new to STL, Could you please help in this regards.
The problem is this line from the header file:
#define iterator
From that point onward, all mentions of the symbol iterator will be that macro. And it will expand to nothing.
So when e.g. std::vector defines its iterator type, it can't.
For header include guards use more complex names. Even using all upper-case (which is what most people uses for macros) would be enough.
Since your header file name is iterator.h (which is really a bad and misleading name) I suggest something like ITERATOR_H instead.
Initial Problem
You've #define iterator meaning that everywhere that the symbol iterator is used it is replaced with, well nothing but this essentially deleted the symbol iterator everywhere it's used meaning that vector and string can't find their iterator types. Try using a different name for the header guard and all capital letters is ideal by convention for macros. Also try giving the header a more complex name as iterator is too generic and could cause future errors. isPalindrone.h would be better.
Eg.
#ifndef IS_PALINDRONE_H
#define IS_PALINDRONE_H
/// ...
#endif
credit #Some programmer dude
Main Question
As for the main part of you're code, using a std::vector<std::string>>. This means that the iterator returned from s1.begin() and s1.end() are iterators to std::string types, not to the elements of the string, ie. the characters of the string. This means that, for example s1.begin() actually returns an iterator to a std::string with the value "Otto" not to a character 'O' like I assume you want.
std::vector is C++'s dynamic array type as opposed to std::string which (as the name suggests) is the most basic string type in C++.
You have two options to fix this. First std::string has iterators of its own which you can pass to you're isPalindrone() function as it stands, or you can create a std::vector<char> and push back each character. This vectors iterators will point to the elements in the vector, ie. iterators to chars. I would personally use a std::string as its easier to manage string types this way but both options work if you were to expand beyond words to say numbers maybe.
Using std::vector<char>
Here's a quick way to make a string into an array of characters.
#include <algorithm>
#include <iterator>
std::string s1 = "otto";
std::vector<char> vs(4); ///< The 4 here is used to pre-allocate memory
std::copy(s1.begin(), s1.end(), std::back_inserter(vs)) ///< Pushes the elements of s1 to the back of vs
Alternative
I know you might be trying to learn iterators and algorithm patterns in C++ but if you want an alternative to approach to achieve the same task, is you can use the std::equal algorithm (found in the <algorithm> header) and std::string's reverse iterators (the .rbegin() and .rend() methods).
#include <algorithm>
#include <string>
/// templated for different string types. Can restrict using concepts (look below)
template<typename StringT>
inline bool isPalindrone(StringT str)
{ return std::equal(str.begin(), str.end(), str.rbegin(), str.rend()); }
Quick Fix for isPalindrone()
One thing you forget to do in you're implementation of isPalindrone() is you don't decrement the end iterator each iteration. By decrementing end each loop you move backwards through the letters of the word, comparing each letter from the front forwards to the back backwards. It also should be noted that anything using templates in C++ must be in a header so the function isPalindrone() must all be in a header.
# First Loop
first
v
Otto
^ end
# Second Loop
first
v
Otto
^ end
Updated Code
template<typename Bidrectional>
inline bool isPalindrone(Bidrectional first, Bidrectional last)
{
while (true)
{
last--;
if (first == last)
break;
if (*first != *last)
return false;
first++;
end--; ///< Decrement `end`
return true;
}
}
Links and Resources
I mentioned a lot so here are links to everything I used to answer the question.
std::vector : cppreference
std::string : cppreference
Note, the link for std::string links to std::basic_string because it is used to implement all standard string types in C++. Scroll down a bit for details.
Iterators Library : cppreference
std::equal : cppreference
std::copy :cppreference
std::back_inserter : cppreference
Concepts Library (if interested) : cppreference Useful to constrain template parameters
If there is anything I missed; edits and constructive comments to help improve the answer would be appreciated.
Little modification #oraqlle solution:-
std::string s1 = "otto";
std::vector<char> vs(4); ///< The 4 here is used to pre-allocate memory
std::copy(s1.begin(), s1.end(), vs.begin());
for (auto v : vs)
{
cout << v << "\t";
}
bool a = isPalindrone(vs.begin(), vs.end());
Related
While migrating from Visual studio 2013 to Visual studio 2019 compiler I have got below error. Please help me in fixing the same.
I have declared the function in the header file (.h) below:
#ifndef CSAHCCOMPOSEDITEM_H
#define CSAHCCOMPOSEDITEM_H
#ifdef _UTEST
class CsaHcDICOMComposerTester;
#endif
class EXP_IMP_HcDicComp CsaHcComposedItem
{
#ifdef _UTEST
friend class CsaHcDICOMComposerTester;
#endif
public:
enum CsaHcComposedItemType
{
CISegment,
CIPage,
CILayout,
CIPageBracket,
CIPrintJobBracket,
CIDummy
};
CsaHcComposedItem
(bool &status, CsaHcComposedItemType type_in);
CsaHcComposedItem
();
CsaHcComposedItem a
(const CsaHcComposedItem& compObj_in);
CsaHcComposedItem& operator=
(const CsaHcComposedItem& compObj_in);
~CsaHcComposedItem();
bool operator==
(const CsaHcComposedItem& ci_in);
private: // attributes
CsaHcComposedItemType
myType;
CsaHcBasicFilmSession
*myBFS;
CsaHcBasicFilmBox
*myBFB;
CsaHcBasicImageBox
*myBIB;
CsaDib *myDib;
BYTE *myPixelArray;
};
#endif // CSAHCCOMPOSEDITEM_H
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And cpp file contains the definition for the constructor.
//pusedo code
CsaHcComposedItem::CsaHcComposedItem(bool &status_out,
// Return status of the construcor
CsaHcComposedItemType type_in)
// Composed item type
: myType(type_in), // error shown for this line (70)
myBFS(NULL), //line71
myBFB(NULL),
myBIB(NULL),
myDib(NULL),
myPixelArray(NULL)
{
.....
}
Error:
1.CsaHcComposedItem.cpp(70): error C2761: '{ctor}': redeclaration of member is not allowed
2.CsaHcComposedItem.cpp(70): error C2059: syntax error: ':'
3.CsaHcComposedItem.cpp(70): error C2065: 'type_in': undeclared identifier
4.CsaHcComposedItem.cpp(70): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
5.CsaHcComposedItem.cpp(71): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
6.CsaHcComposedItem.cpp(72): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7.CsaHcComposedItem.cpp(73): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8.CsaHcComposedItem.cpp(74): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
9.CsaHcComposedItem.cpp(75): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
10.CsaHcComposedItem.cpp(78): error C2448: 'myPixelArray': function-style initializer appears to be a function definition
That source is compiled without error in VS2019(16.4.5)
I compiled with these declarations
#include <Windows.h>
#define EXP_IMP_HcDicComp
using CsaHcBasicFilmSession = int;
using CsaHcBasicFilmBox = int;
using CsaHcBasicImageBox = int;
using CsaDib = int;
Issue is fixed,
below line of code was not commented in my cpp file, after commenting it worked.
I get errors compiling the code below when ugrading from VS 2008 to VS2015. The code is from the com4j project. Help wanted. Thanks!
syntax error: missing ';' before '<' missing type specifier
int assumed. Note: C++ does not support default-int
'array': ambiguous symbol
unexpected token(s) preceding ';'
Code:
// Class to marshal SAFEARRAY to Java multi dimensional array
//
// itemType : array item type
// XDUCER : converter for each array item
template < VARTYPE itemType, class XDUCER >
class ToJavaMultiDimlArrayMarshaller {
typedef array::Array<typename XDUCER::JavaType> JARRAY; // Errors here
typedef SAFEARRAY* NativeType;
typedef jarray JavaType;
What does VARTYPE stand for? Is it a macro? Replacing with class or typename may help
so i'm doing a uni project and when i try to compile a weird error pops up.I've read it might be from circular dependencies but i've checked and thats not the problem here's the header code :
#ifndef __ScheduleHeader_H_INCLUDED__
#define __ScheduleHeader_H_INCLUDED__
#include "TrainStationHeader.h"
class Schedule
{
friend class ScheduleIterator;
public:
//get data functions
std::string getFromCity() { return fromCity; }
std::string getToCity() { return toCity; }
std::string getTrainID() { return trainID; }
std::string getDate();
std::string getTime();
std::string getTimeForTickets();
std::string getPrice() { return price; }
//set data functions + validation on the input data
bool setFromCity();
bool setToCity();
void setDate();
void setTime();
bool setTrainID();
void setPrice();
std::string createScheduleLine();
private:
int validateDate(struct tm,struct tm);
int validateTime(struct tm,struct tm);
std::string scheduleFileName;
std::string fromCity;
std::string toCity;
struct tm dateAndTime;
std::string trainID;
std::string price;
};
#endif
I've added the friend because i have a Schedule object in another class as private.The error when i compile is :
c:\program files (x86)\windows kits\8.0\include\um\schedule.h(60): error C2146: syntax error : missing ';' before identifier 'Type'
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(60): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(61): error C2146: syntax error : missing ';' before identifier 'Offset'
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(61): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(68): error C2146: syntax error : missing ';' before identifier 'Size'
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(68): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(69): error C2146: syntax error : missing ';' before identifier 'Bandwidth'
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(69): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(70): error C2146: syntax error : missing ';' before identifier 'NumberOfSchedules'
1>c:\program files (x86)\windows kits\8.0\include\um\schedule.h(70): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
PS:I'm using MC VS 2012
here's the TrainStation.h , Its just a bunch of functions and libraries i use across the majority of the classes (i know it's not the best practice) :
#ifndef __TrainStation_H_INCLUDED__
#define __TrainStation_H_INCLUDED__
#include <iostream>
#include <string>
#include <fstream>
#include <locale>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <vector>
std::string removeWhiteSpace(std::string a);
//Resieves the file on wich to operate upon and what to search for.
//If it finds it it returns the line on wich it has been found ,if not returns -1
int checkContent(std::string FileName, std::string target);
//Give the Function the File Name with wich you would like to work and the target that you would like to delete
//It works by copying everything exept the target string to another file then renames it and deletes the old file
int deleteContent(std::string File,std::string target);
void renameFile(std::string , std::string);
bool checkFileState(std::ifstream &file);
#endif
the error message indicates error in "C:\program files (x86)\windows kits\8.0\include\um\schedule.h" that is part of installation of VS or additional SDKs. You quote a completely different header file.
If you can't figure out the error, ask for preprocessor output and read that to see the problem. /SHOWINCLUDES may also help.
I use Visual Studio 2005
When I compile, I get this error:
Error 1 error C2146: syntax error : missing ';' before identifier 'mDropEndTime'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
struct MB_SYN_DROPITEM_UPDATE : MSGBUF
{
long mCid; // Index
unsigned long mItemIdx; // idx
TIMESTAMP_STRUCT mDropEndTime; // This is error line
};
Why doesn't C++ know TIMESTAMP_STRUCT?
TIMESTAMP_STRUCT is something defined in sqlext.h
You must add
#include <sqlext.h>
Because TIMESTAMP_STRUCT is not part of the C++ standard.
I need to refactor a .dll for a Zinc based Flash application.
After copy&paste a class from the master to the branch, I'm getting some strange compiling errors:
GameInfo.h(15): error C2146: syntax error : missing ';' before identifier 'm_wsVersion'
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The addressed code:
// removed the comments
#include "stdafx.h"
#include <string.h>
class GameInfo {
public:
UINT m_uiGameId;
wstring m_wsVersion; // Line 15
UINT m_uiCheckSum;
wstring m_wsFilePath; // Same error report as on line 15
public:
static BOOL createFromFile(wstring path, GameInfo &target); // error "error C2061: syntax error : identifier 'wstring'" thrown
};
I use Visual Studio 2010 and in the IDE itself everything is okay, no syntactical errors or something like that. And as said I did not touch the code, headers seem fine.
Has anyone a clue what about this error?
Try using the string header, and qualifying the namespace:
#include <string>
class GameInfo {
....
std::wstring m_wsVersion;
};
#include <string> is the right standard include in C++ for string classes and use std::wstring.
I strongly recommend AGAINST using a using namespace std; inside one of your headers, as you would force anybody using the header to pull in the std stuff into the global namespace.