C++ compiling errors [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
First of all here is my C++ source:
#include <iostream>
using namespace std;
void number(int x){
cout << "Number is: " << x << endl;
}
int main(){
cin >> int x;
number(x);
return(0);
}
Upon compiling I get the following errors:
file.cpp: In function 'int main()':
file.cpp:9:9: error: expected primary-expressing before 'int'
file.cpp:9:9: error: expected ';' before 'int'
file.cpp:10:9: error: 'x' was not declared in this scope
I compile and run this successfully in CodeBlocks but under Ubuntu with gcc or g++ things fail.

Change
int main(){
cin >> int x;
to
int main(){
int x;
cin >> x;

Related

Getting a compilation error while using (<=>) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
New to C++ here, following outdated training videos...
Trying to compile this simple code, but getting the below error:
#include <iostream>
int main(){
auto result = (10 <=> 20) > 0;
std::cout << result << std::endl;
}
Error I get is:
prog.cc: In function 'int main()':
prog.cc:4:25: error: expected primary-expression before '>' token
4 | auto result = (10 <=> 20) > 0;
|
What am I doing wrong?

Problems with VS Code saying endl,cout and sin was not declared in this scope [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Here is my code
#include <iostream>
using namespace std;
int main() {
int age;
cin >> age;
cout << "Age is " << age << endl;
return 0;
}
Whenever I run the program, I get errors saying that 'cout', 'endl' and 'cin'are not declared in this scope. I looked up this problem online and I made sure I had the "using namespace std;".
Another post mentioned this was a bug and said that to fix this bug I would change
""C_Cpp.intelliSenseEngine": "Default" to "C_Cpp.intelliSenseEngine": "Tag Parser"."
I did this and it still doesn't work. Anyone have any ideas?
Edit: Here is the Error Code:
PS C:\School\C++\C++ VSD> cd "c:\School\C++\C++ VSD\" ; if ($?) { g++ test.cpp -o test } ; if ($?) { .\test }
test.cpp:3:2: error: invalid preprocessing directive #using
#using namespace std;
^~~~~
test.cpp: In function 'int main()':
test.cpp:7:5: error: 'cin' was not declared in this scope
cin >> age;
^~~
test.cpp:7:5: note: suggested alternative:
In file included from test.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:60:18: note: 'std::cin'
extern istream cin; /// Linked to standard input
^~~
test.cpp:8:5: error: 'cout' was not declared in this scope
cout << "Age is " << age << endl;
^~~~
test.cpp:8:5: note: suggested alternative:
In file included from test.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:61:18: note: 'std::cout'
extern ostream cout; /// Linked to standard output
^~~~
test.cpp:8:33: error: 'endl' was not declared in this scope
cout << "Age is " << age << endl;
^~~~
test.cpp:8:33: note: suggested alternative:
In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from test.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:590:5: note: 'std::endl'
endl(basic_ostream<_CharT, _Traits>& __os)
You have to add #include <iostream> on the beginning of your file. then it should work properly

Ambiguating new declaration of 'void visualizar()' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new in C++ and i have a little error in this program "Hello world"
//ejemplo funciones definidas por el usuario
#include<iostream>
using namespace std;
int visualizar();
int main()
{
visualizar();
return 0;
}
void visualizar() //Here is the error
{
cout<<"Hola mundo guay\n";
}
Error:
C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp In function 'void visualizar()':
15 17 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Error] ambiguating new declaration of 'void visualizar()'
6 5 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Note] old declaration 'int visualizar()'
What does this error mean? What caused it? How do I fix it?
In your prototype of visualizar, you declared it as
int visualizar();
However, when you defined it, you wrote
void visualizar() { ... }
Notice that the return types are different. Did you mean to use void throughout?

unexpected compilation error in a function call (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a template function makeMatrix(), code is:
template<size_t N>
void makeMatrix(string dataFilePath, int adjcMatrix[N][N])
{
fstreamExtension fe("adj.txt", ios::in|ios::binary);
string s;
vector<int> temp;
int i = 0;
while(!fe.eof())
{
getline(fe, s);
temp = tokenizeToInt(s, ",\n")); //error: expected ';' before ')' token|
for(int j = 0; j < N; j++)
adjcMatrix[i][j] = temp[j];
i += 1;
}
}
fstreamExtension is a class I created and is included in program through header
#include "fstreamExtension.h", other included headers are iostream string and boost/tokenizer.hpp.
code for tokenizeToInt():
vector<int> tokenizeToInt(string& intString, const char* seperators)
{
vector<int> intValues;
boost::char_separator<char> delims(seperators);
boost::tokenizer<boost::char_separator<char>> tokens(intString, delims);
for (const auto& t : tokens) {
intValues.push_back(atoi(t.c_str()));
}
return intValues;
}
Why it is causing a compilation error in the makeMatrix(), the syntax seems correct, I didn't called it in main(), was compiling some other code then this error popped up when I started a build.
IDE : codeblocks 16.01, gcc.
You should listen to what the compiler tells you. Often the error is simpler than you think:
temp = tokenizeToInt(s, ",\n")); //error: expected ';' before ')' token|
An extra right-parenthesis. The compiler error means "I thought you were done with this command, why are you trying to close another parenthesis-pair?"

Understanding errors in printing a Linked list in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have recently worked on C, but I have started to study C++. I had a homework in to create a program that would read texts and organize data out of the imputed text. This is the last part I have left, but I don't get what's wrong with my code. This part of the problem is pretty simple, but I still don't understand what my errors are. I got used to gcc compiler which wrote mostly segmentation fault, but g++ compiler errors are different. Any tips or hints on what to pay more attention while transferring from c to c++ would be really appreciated.
This is my output errors.
-bash-3.2$ g++ -o Printfunction Printfunction.cpp
Printfunction.cpp: In function 'void Printfunction(wordList*)':
Printfunction.cpp:43: error: cannot convert 'NumberList*' to 'Numberlist*' for argument '1' to 'std::string returnlist(Numberlist*)'
Printfunction.cpp: In function 'std::string returnlist(Numberlist*)':
Printfunction.cpp:56: error: invalid use of undefined type 'struct Numberlist'
Printfunction.cpp:10: error: forward declaration of 'struct Numberlist'
Printfunction.cpp:56: error: 'to_string' was not declared in this scope
Printfunction.cpp:57: error: invalid use of undefined type 'struct Numberlist'
Printfunction.cpp:10: error: forward declaration of 'struct Numberlist'
Can you please tell me what's wrong with my code?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct NumberList
{
int line;
struct Numberlist *nextPtr;
};
struct wordList
{
string word;
int Count;
NumberList lines;
struct wordList *nextPtr;
};
void Printfunction(wordList *list);
string returnlist(Numberlist *list);
int main()
{
wordList something;
something.word = "SOMETHING";
something.Count = 55555;
something.nextPtr = NULL;
Printfunction(&something);
}
void Printfunction(wordList *list)
{
int i;
i=1;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
cout<<"|# | WORD | COUNT | LINES |"<<endl;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
while(list != NULL)
{
cout<<"|"<<left<<setw(4)<<i<<"|"<<left<<setw(28)<<list->word<<"|"<<left<<setw(7)<<list->Count<<"|"<<left<<setw(33)<<returnlist(&(list->lines))<<"|"<<endl;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
list = list->nextPtr;
i++;
}
}
string returnlist(Numberlist *list)
{
string final;
while(list != NULL)
{
final.append(", ");
final.append(to_string(list->line));
list = list->nextPtr;
}
final.append(".");
return final;
}
The problem is that sometimes you spell it NumberList, and sometimes you spell it Numberlist.
Any tips or hints on what to pay more attention
Case matters.