#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
const int size = 15;
char array1[size];
char array2[size] = "four";
cout << array2[3]='\0' << endl;
return 0;
}
Hello, I am new to C++, I am learning the C-string, my question is why
array2[3] ='\0' can not be use inside the cout, the error I get is:
reference to overloaded function could not be resolved.
It works fine if it's outside of cout. thank you :)
Related
Whenever I use std::string to declare a string variable in c++ it prevents the program from outputting anything. for example:
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
// std::string s;
return 0;
}
This will output Hello to the command-line as it should do.
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
std::string s;
return 0;
}
This will not output anything (and no errors) since i'm declaring a variable using std::string
I'm using the minGW compiler on a Windows 10 64bit machine
I am creating a small C++ program for homework. I im trying to populate a 2D vector but when I write matriz[iA][iB]=iNum; it gives me the error "no match for 'operator='"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
using namespace std;
void popularMatriz(int iTamano, vector<vector<int>> *matriz){
for(int iA=0; iA<iTamano; iA++){
for(int iB=0; iB>iTamano; iB++){
int iNum;
scanf("%d", &iNum );
matriz[iA][iB]=iNum;
}
}
}
int main(){
int iTamano;
scanf("%d", &iTamano);
vector<vector<int>> matriz(iTamano, vector<int>(iTamano));
matriz[2][2]=5;
popularMatriz(iTamano, &matriz);
return 0;
}
You're passing a pointer to matriz; so is wrong use it as
matriz[iA][iB]=iNum;
I suggest you to pass it as reference; I mean, define popularMatriz() as
void popularMatriz(int iTamano, vector<vector<int>> & matriz)
and call it without &
popularMatriz(iTamano, matriz);
You're taking a pointer to matriz. Do this instead:
(*matriz)[iA][iB]=iNum;
I am getting a bunch of errors saying "Undefined reference to ...", and cant understand why. I have read other questions with the error "Undefined reference to ...", but the answers doesent work for me. Here is part of my code:
main:
#include <iostream>
#include <armadillo>
#include <string>
#include <DombsMain.h>
using namespace std;
using namespace arma;
int main(){
cout << "Armadillo version: " << arma_version::as_string() << endl;
dombsmain::initilize("test");
return 0;
}
dombsmain.h:
#ifndef DOMBSMAIN_H_INCLUDED
#define DOMBSMAIN_H_INCLUDED
#include <string>
#include <vector>
#include "Body.h"
#include "Constraint.h"
#include <Solver.h>
namespace dombsmain {
extern void initilize(std::string fileName);
extern std::string inputfileName;
...blabla
}
#endif // DOMBSMAIN_H_INCLUDED
dombsmain.cpp:
#include <DombsMain.h>
#include <BallJoint.h>
#include <dombs.h>
#indlude blabla
using namespace std;
using namespace arma;
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
..blabla
I think the error has something to do with namespace dombsmain. DombsMain.h is included both in main and in dombsmain.cpp, but it still says that the variables and functions in the namespace in undefined. I think it might be some conflict with including DombsMain.h int both main and dombsmain.cpp. I tried deleting the #include <DombsMain.h> in main.cpp, but then I couldent call dombsmain::initilize("test");
You just forgot to define std::string inputfileName. For example you can do it in dobsmain.cpp:
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
}
std::string inputfileName = "";
}
I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.
I wrote the following C++ program, but at the line where I used out_stream.open(), it keeps telling me that there are errors about "unknown typename 'out_stream'" and "Expected unqualified-id".
I am new to C++ and I think I just copied down the lines from my textbook, so I can't figure out where it's wrong. Please bear with me if it is a really simple mistake.
Here's my code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/math/distributions.hpp>
std::ofstream out_stream;
out_stream.open("output.txt");
int main()
{
std::cout<<"hello world!";
return 0;
}
You cannot to this
out_stream.open("output.txt");
outside of a function. Put it inside the main().
int main()
{
out_stream.open("output.txt");
std::cout<<"hello world!";
return 0;
}