Assigning Char Array to Value

Hello I am having trouble declaring a variable "answerArray" in my structure for my code. This is the code I have written:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Scantron {
    char Name[21];
    char Id[11];
    char CRN[6];
    char testCode[3];
    char specialCode[4];
    char score[4];
    char answerArray[62] = " ";
};


struct csvRecord {
    char Name[21];
    char Id[11];
    char score[4];
    char answerArray[124] = " ";
};


When I run it through a clang++ compiler it generates this error and I'm not sure how to fix it:

fileTransform2.cpp:34:26: warning: in-class initialization of non-static data
member is a C++11 extension [-Wc++11-extensions]
char answerArray[62] = " ";
^
fileTransform2.cpp:42:27: warning: in-class initialization of non-static data
member is a C++11 extension [-Wc++11-extensions]
char answerArray[124] = " ";
^
2 warnings generated.
/tmp/fileTransform2-8559f3.o: In function `main':
fileTransform2.cpp:(.text+0x1c3): undefined reference to `readData(std::basic_ifstream<char, std::char_traits<char> >&, Scantron&)'
fileTransform2.cpp:(.text+0x2e4): undefined reference to `writeData(std::basic_ofstream<char, std::char_traits<char> >&, csvRecord)'
fileTransform2.cpp:(.text+0x30a): undefined reference to `readData(std::basic_ifstream<char, std::char_traits<char> >&, Scantron&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Last edited on
You probably have to enable C++11 (or later) support in your compiler. A command-line option, perhaps?

Why do you have character arrays at all? Why don't you use std::strings?
They were predefined for us by our instructor so I'm not sure why but she wanted us to use char. Do you know what the error means when it says "undefined reference"? I figured out how to compile it with the command "clang++ -Wall -std=c++11 filename.cpp" to clear the first two errors
If I'm not mistaken, the error means that the functions readData() and writeData() are not declared in the scope where you tried to call them in fileTransform.cpp.
No.
linker command failed

Linker error.

In the compilation process the compiler does first generate an object file for each translation unit and then the linker creates a binary from the object files and libraries.

The main() has a call to readData(). The main() is in fileTransform2.cpp. There is suitable declaration of readData() too and thus the compiler had no problems in compiling the fileTransform2.cpp.

The linker does not find the object code of readData().

Either
you have no implementation for readData( std::ifstream&, Scantron& )
Or
it is in a different .cpp that was not compiled, or at least was not included in the linking



PS. Would you explain what happens in char answerArray[62] = " ";
(It is trivia, but it is good to test the extent of your knowledge occasionally?)
Last edited on
C structs with C strings are easier and more efficient to serialize and no less efficient when assigning the values or working with them for most tasks. Whether that is justifiable here or not, or whether its just another case of professors using char arrays, I cannot say.
Topic archived. No new replies allowed.