string char being assigned to a char - c++

Just recently began to get back to programing and started doing some exercises but i keep gettin an error that should be simple to solve but cant seem to solve it...
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
int numberInts = 3;
int strSize = 0;
char interator = 'o';
string source[3];
string strTest ("this is a test");
//source = (string*) malloc (3+1);
source[0] = "(a+(b*c))"; //abc*+
source[1] = "((a+b)*(z+x))";
source[2] = "((a+t)*((b+(a+c))^(c+d)))";
for(int i=0;i<numberInts;i++)
{
strSize = source[i].size();
for(int j = 0; j < strSize; j++)
{
iterator = strTest[0];
if(source[i][j] == '\(')
{
cout<<"\(";
}
}
cout << "\n";
}
return 0;
}
the line "iterator = strTest[0];" gives me a missing template argument error, and i cant really figure out why cant i assign to a char a position of a string that returns a char...
thanks

For one thing, you misspelled iterator as interator when you declared it.

Spelling mistake, your char variable is called interator not iterator.

Switch to Clang. It's error messages are much more specific. It actually catches most spelling mistakes and offers suggestions to what it thinks you might have meant. However it probably wouldn't have caught this as a spelling error because of the iterator template.
You would have seen the following as an error:
testclang.cpp:8:5: error: cannot refer to class template 'iterator'
without a template argument list
iterator = 5;
^
In file included from testclang.cpp:1:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:67:
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:103:12: note:
template is declared here
struct iterator
However without the `using namespace std', this (testclang.cpp):
int main()
{
int interator = 3;
iterator = 5;
}
When compiled with clang:
clang testclang.cpp
produces:
testclang.cpp:4:5: error: use of undeclared identifier 'iterator'; did
you mean 'interator'?
iterator = 5;
^~~~~~~~
interator

Related

GCC template : error : expected ',' or '...' before '< ' token

I'm a new bee to C++.
I'm having below errors when tying to compile below templates example copied from "Essential C++", I simply copied the example on the book.
"error : expected ',' or '...' before '< ' token"
"error : 'vec' was not declared in this scope
I was wondering what could be the problem, could you help spare some time to give a hint? so much appreciate!
'''
#include <iostream>
template <typename elemType>
void display_message(const string &msg, const vector<elemType> &vec)
{
cout << msg;
for(int ix = 0; ix < vec.size(); ++ix)
{
elemType elem = vec[ix];
cout << elem << ' ';
}
}
int main()
{
int size = 10;
ocnst vector<int> ivec1 = fibon_seq(5);
if (is_size_ok(size))
{
display_message(msg, size);
}
display_message(msg, ivec1);
cout << "Hello world!" << endl;
return 0;
}
'''
There's a lot of issues with the code that prevent from compilation:
The functions fibon_seq and is_size_ok are not defined. You have to define them in this file or include them with a #define macro.
The code appears to be including the std namespace. You need to include this with using namespace std;, usually at the beginning of the code, after library includes.
The variable msg is passed as an argument in a function, but it's not defined.
This function call display_message(msg, size); is being passed the wrong argument. size is an integer, but the function expects vector<int>.
5.ocnst is a typo. It should be const.
There are a few problems with your code:
ocnst should be const.
Missing #include <string>
Missing #include <vector>
The last one is the likely cause of that particular error.
Thank you so much for your time and generous help!H
How embarrassed, I did forgot to add
#include since I copied the code from previous code.
"essential C++" seems a good book that recommend by many ppl. I'm still trying to familalize the language.
if you think there is better material, please give me a piece of advise, Thanks!

Why am I getting a <function> is not a member of <class>? I was trying to pass a string and return from it

I am trying to pass a string to a function, which is going to be sorted. Then to have the sorted string to be return from it. It won't compile, and it even says that "binarySort is not a member of Others".
Is it something that's not allowed to do?
This is the Class File
#include "Others.h"
#include <iostream>
using namespace std;
char Others::inputCheck(char guess) {
while (!isalpha(guess)) { //If the inputs are not alphabets, keep looping.
cout << "Enter again: ";
cin >> guess;
}
cin.ignore(1, '\n');
guess = tolower(guess);
return guess;
}
string Others::binarySort(string sampleText) {
//Bubble sorting the string. (Copy pasted)
for (int i = 0; i < sampleText.length(); i++)
{
for (int j = i + 1; j < sampleText.length(); j++)
{
if (sampleText[i] > sampleText[j]) //if previous has bigger ascii value than next,
{
//swapping the prev and next characters
char temp = sampleText[i];
sampleText[i] = sampleText[j];
sampleText[j] = temp;
}
}
}
return sampleText;
}
This is the Header File.
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
class Others
{
public:
char inputCheck(char guess); //Function to check if inputs are alphabets.
string binarySort(string sampleText);
};
#endif
The main function.
#include <iostream>
#include "Others.h"
using namespace std;
int main() {
string sortedText, sampleText = "toibeawn";
Others sorter;
sortedText = sorter.binarySort(sampleText);
cout << "Text after sorted:\n";
cout << sortedText;
return 0;
}
The sort works if it's not used for a function.
The error output:
1>Sorting_test.cpp
1>Others.cpp
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,7): error C2039: 'string': is not a member of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C3646: 'binarySort': unknown override specifier
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C2059: syntax error: '('
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,30): error C2039: 'string': is not a member of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,48): error C2238: unexpected token(s) preceding ';'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.cpp(16,21): error C2039: 'binarySort': is not a member of 'Others'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(5): message : see declaration of 'Others'
1>Generating Code...
1>Done building project "Sorting_test.vcxproj" -- FAILED.
You include "Others.h" prior to declaring using namespace std which results in string being unrecognized in the included header.
Add using namespace std; in the header "Others.h" and the error will be gone.
Generally, it is a bad practice to have using namespace std in headers and you'd better just write std::string.
Everything is fine. Require changes a simple change.
in your header include this header : #include and use std namespace.
So after changes, your header looks like
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
#include <iostream>
using namespace std;
The main problem is, in your header the string is not resolved due to the lake of string header and std namespace.
Note: Always try to include language headers first and then your custom headers.
std::string binarySort(std::string sampleText)
Needed a namespace

Errors with declaring/defining and vectors

I'm working on some code and I've come across a few errors to do with defining/declaring and expecting a type specifier where my class name is?
I was wondering if anyone can simply explain to me where I've gone wrong and how to resolve these issues?
I've commented out the errors on each lines, Boot and Shoe and Footwear are classes in a header file Footwear.h
#include <iostream>
#include <string>
#include <vector>
#include "typedefs.h"
#include "Footwear.h"
int main(int argc, const char * argv[])
{
vector<Footwear*> collection; // vector and Footwear are undefined
Footwear* f; //f undefined
f = new Boot("Timbaland",10); //expected a type specifier
collection.push_back(f); //collection undefined
f = new Shoe("Brogue",5); //expected a type specifier
collection.push_back(f);
for (i = 0; i < collection.size(); i++) //i undefined
collection[i]>toString(); //toString undefined
return 0;
}
Use: std::vector
That may clear all the warnings. You've included but you still need to prefix the std namespace.
In your for loop, you must declare for(int i = 0....) you may want to use unsigned int since collection.size() should never be < 0.

sending a list of string from C# to C++ causes errors

I want to send a C# a list of string into C++ code using C++/CLI:
in C++, I put this in a constructor:
#include <string>
public:
MyAlgorithm(array<std::string>^ listAlgorithms);
But I got this compilation error:
error C2691: 'std::string' : a managed array cannot have this element
type
And in the implementation I have:
MyAlgorithm(array<std::string>^ listAlgorithms)
{
pin_ptr<std::string> algorithms = &listAlgorithms[0];
std::string* unmanagedAlgorithms = algorithms;
}
And I got this error:
error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'cli::pin_ptr<Type>'
How should I correct them?
Thanks in advance.
#include <string>
#include <msclr/marshal_cppstd.h>
MyAlgorithm(array<String^>^ listAlgorithms)
{
std::vector<std::string> unmanagedAlgorithms(listAlgorithms->Length);
for (int i = 0; i < listAlgorithms->Length; ++i)
{
auto s = listAlgorithms[i];
unmanagedAlgorithms[i] = msclr::interop::marshal_as<std::string>(s);
}
}
or
std::vector<std::string> unmanagedAlgorithms;
for each (auto algorithm in listAlgorithms)
{
unmanagedAlgorithms.push_back(msclr::interop::marshal_as<std::string>(algorithm));
}
or first string only
String^ managedAlgorithm = listAlgorithms[0];
std::string unmanagedAlgorithm = msclr::interop::marshal_as<std::string>(managedAlgorithm);
Y'all can't use classes when defining a managed array. If you're looking to use the std::string class, you're probably best going with something like std::vector.
PS: How come you don't do?
using namespace std;

error: expected `;' before '{' token - What is the cause?

Here is my implementation file:
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
//----------------Constructor----------------//
Canvas::Canvas() //line 10
{
Title = "";
Nrow = 0;
Ncol = 0;
image[][100]; // line 15
position.r = 0;
position.c = 0;
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
The errors I'm getting are these:
proj05.canvas.cpp: In function 'std::istream& operator>>(std::istream&,
Canvas&)':
proj05.canvas.cpp:11: error: expected `;' before '{' token
proj05.canvas.cpp:22: error: a function-definition is not
allowed here before '{' token
proj05.canvas.cpp:24: error: expected `}' at end of input
proj05.canvas.cpp:24: error: expected `}' at end of input
These seem like simple syntax errors, but I am not sure what's wrong. Could someone decode these for me? I'd really appreciate it, thanks for your time!
EDIT
Here is the definition of Canvas in my .h file:
#ifndef CANVAS_H
#define CANVAS_H
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
class Canvas
{
public:
Canvas(); void Paint(int R, int C, char Color);
const int Nrow;
const int Ncol;
string Title;
int image[][100];
stack<int> path;
struct PixelCoordinates
{
unsigned int r;
unsigned int c;
} position;
};
#endif
"proj05.canvas.h" i bet the problem is there. may be no ; after class def
You must use initializer list to initialize const-members
Try this:
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
using namespace std;
//----------------Constructor----------------//
Canvas::Canvas():Nrow(),Ncol() // Initializer list
{
Title = "";
//initialize image[][] correctly, your way is syntactically incorrect
position.r = 0; //correction here
position.c = 0; // and here
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
Few things:
1
PixelCoordinates.r = 0;
PixelCoordinates.c = 0;
should be:
position.r = 0;
position.c = 0;
2
image has already been declared. What is this:
image[][];
It sounds like you forgot to put a semicolon after your class definition. Look in "proj05.canvas.h". You should see something like:
class Canvas{
...
};
One thing that catches my eye as wrong/weird is image[][]. That does not really do anything. Also, I do not believe you can assign to constant member outside of a ctor list.
Finally, your assignment to PixelCoordinates is completely in error. You've created a local struct definition, but have not made a member that uses it, therefore you cannot assign anything at all to it - especially the struct's title. That would really confuse a compiler.
Yikes.
(Not an answer to your specific problem, but...)
You should also remove the
using std;
That has no business in a .h file.
I am going to guess the oddly formatted .h file may be a problem. It is legal for a filesystem of course, but it could be that. Also ensure you have the ending semicolon on the class.
You need to have both dimensions filled in for the array you have (probably a horrible design to use that int he class anyway...)
Whatever the reason for other errors is, the memeber definition int image[][100] is illegal. Non-static data members of the class cannot be declared with incomplete types. All dimensions of an array must be specified explicitly.